Tutorial

How To: Publishing Your First Package to npm

Published on December 16, 2019
    Default avatar

    By William Le

    How To: Publishing Your First Package to npm

    While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.

    Here’s a little guide for publishing your own packages on npm.

    You’ve written some piece of software code that you think is really useful! Now you want publish it on npm so that others can use your wonderful bundle of code!

    npm xmas

    Firing Up the Terminal

    There’s a lot of configurations for npm but many times these will be project-specific. This article is just going to cover the essentials for getting your packages published on npm.

    npm is included with Node.js. To check if npm is installed on your system, run this command in your terminal: npm -v

    Make the Directory

    Let’s create a folder that’s going to hold our package’s source code. In your terminal:

    # This will create, and navigate
    # into the `wonderful-bundle` directory
    $ mkdir wonderful-bundle
    $ cd wonderful-bundle
    

    Initializing a npm Package

    Now that you’re in the folder, here’s where we start using npm commands!

    $ npm init
    

    Running npm init will ask you a few setup questions (for example, the name of your package, your package’s description, etc).

    You can simply hit “Enter” for each question and this default boilerplate for package.json file will be created in your directory:

    package.json
    {
      "name": "wonderful-bundle",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
          "test": "echo \"Error: no test specified\" && exit 1"
      },
      "author": "",
      "license": "ISC"
    }
    

    What is the package.json file?

    You can compare the package.json file to a recipe card for your favorite meal.

    The package.json file contains all of the descriptive metadata about the project (like the name “Apple Crumb Pie”), and all of the dependencies needed to properly run it (the ingredients: “apples”, “pie crust”, “sugar”, etc).

    Using all of this info, npm brings everything together so that your package can be effortlessly downloaded & run by other people.


    Let’s edit package.json to include a description, and author information.

    {
      "name": "wonderful-bundle",
      "version": "1.0.0",
      "description": "outputs an uplifting message",
      "main": "index.js",
      "author": "Chompy MacPherson <chompymac@alligator.io>",
      "license": "ISC"
    }
    

    The only fields that are required in package.json are “name”, “version”, and “main”. The “scripts” field was removed since we don’t have any tests written yet.

    The “main” field is the file path to the JavaScript code. When someone uses your package this JavaScript file will be used. Let’s create the index.js file in our terminal:

    $ touch index.js
    

    In a text editor of your choice…

    index.js
    module.exports = function() {
      console.log("you're wonderful!");
      return;
    };
    

    Remember to export your code in the same way you would for files that are local to your software project.

    Creating a README

    Generally it’s a good idea to include documentation for your package so others know how to use it. The README file is typically used for this purpose.

    Let’s create the README file in the root of your package’s directory:

    # create the README file
    $ touch README
    
    # put some text into README
    $ echo "## Wonderful Bundle \n\n Get an uplifting message!" > README
    

    …and Publish

    Currently, this is what the file directory for wonderful-bundle looks like:

    wonderful-bundle
     |_ index.js
     |_ README
     |_ package.json
    

    Essentially, this is the basic structure of a npm package. Not much is needed to publish your software to npm!

    Now that we feel pretty good about our package, let’s publish it!

    $ npm publish
    

    You’ll need an account on the npm registry website, and if you’re not logged into it from the CLI you’ll be asked to log in. You also have to use a package name that hasn’t been used on the registry already.

    Wrapping Up

    That’s it! Your package is now published on npm. To recap, there are only 3 steps to go from zero-to-published:

    • Initialize: npm init
    • Add source code: index.js and README
    • Publish: npm publish

    Now when someone wants to use your package, they just run this in their terminal:

    $ npm install wonderful-bundle
    

    This will download, and install any dependencies needed for your package in other people’s software projects! Technology is amazing 🤤


    Hopefully this guide has shown you how easy it is to contribute your software to the Open Source community, regardless of how significant or small it might be 📦 👉 🌎

    If you prefer using Yarn check out this guide: npm vs Yarn Commands Cheat Sheet

    Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about us


    About the authors
    Default avatar
    William Le

    author

    Still looking for an answer?

    Ask a questionSearch for more help

    Was this helpful?
     
    Leave a comment
    

    This textbox defaults to using Markdown to format your answer.

    You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    Join the Tech Talk
    Success! Thank you! Please check your email for further details.

    Please complete your information!

    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

    Become a contributor

    Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

    Learn more
    DigitalOcean Cloud Control Panel