How to publish packages to npm (the way the industry does things)

Zell Liew
Zell Liew
Apr 9 · 6 min read

It’s simple to publish a package onto npm. There are two steps:

  1. Create your package.
  2. Publish the package.

But publishing packages the way the industry does it? Not so simple. There are more steps. We’ll go through what steps are required, and I’ll show you an easy way to publish and update your package.

Creating your first package

This section is for you if you haven’t published a package to npm before. Feel free to skip to the next section if you published one before.

To publish your first package to npm, you need to go through these steps:

First, you need to have an npm account. Create one here if you don’t have one yet.

Second, you need to login to your npm account through the command line. (You need to have Node and npm installed on your system before you perform this step. Install them here).

To sign in, you use .

npm login

You’ll be prompted to enter your username, password, and email address.

Third, you need to create a package. To do so, create a folder somewhere on your computer and navigate to it. The command line version is:

# Creating a folder named how-to-publish-to-npm mkdir how-to-publish-to-npm # Navigating into the folder cd how-to-publish-to-npm

Next, you want to begin the project with the command.

npm init

This command runs you through a few questions and creates a file for you at the end. This file contains the bare necessities you need to publish your project. (Feel free to skip questions that don’t make sense).

The final step is to publish your package with the command.

npm publish

If the package already exists on npm (because your package has the same name as another package on npm), you won’t be able to publish it. You’ll get an error.

You’ll need to change your package name.

To change your package name, you change the property in the file. Here, I changed it to .

(You can check for naming collisions by doing a search on npm, or through the command).

It’s also a good idea to update the folder name as well for consistency. Here’s the command line equivalent.

# Command to change folder names by moving everything mv how-to-publish-to-npm publishing-to-npm

Try the command again. You should get a success message now.

What to do if every name you came up with is taken already

This is a common problem since many people create packages on npm. It’s difficult to get the package name you desire sometimes. (It’s like how I can never find a good domain).

To combat this problem, npm lets you publish to a scope. This means you can publish the package under your own username (or npm organization), so you’re free from naming problems.

To publish to a scope, you can either:

  1. Change the to manually in
  2. Run instead of

If your repository has a scope, you need to adjust the publish command slightly:

npm publish --access public

That’s all you need to do to publish a package to npm.

Now, let’s move on to how the industry publishes packages.

Consider a popular framework like React. If you dig around React, you’ll notice a few things:

First, React has a Github repository.

Second, React is published on npm.

Third, React follows Semantic versioning (Semver for short).

Fourth, each update to React has a git tag associated with it. This git tag follows Semver as well.

Fifth, there are release notes for every React update.

This means publishing a package involves many steps. At the very least, you need to:

  1. Run tests (if there are any)
  2. Update in according to Semver
  3. Create a git tag according to Semver
  4. Push the package to Github
  5. Push the package to npm
  6. Create release notes for every update

It’s common to forget one of these things when we’re ready to push. Sometimes we and we enjoy a break. When we’re back, we screw ourselves for forgetting.

There’s an easier way. It’s with a tool called .

np

np (created by Sindre Sorhus) makes it easier for us to publish packages without missing any of the steps I detailed above.

Installing np

To install , you can run the following command:

npm install np

This works. But I prefer installing globally on my computer so I can run the command anywhere.

sudo npm install --global np

Before using np

Before you use you need to make sure:

  1. Your project is a Git repository
  2. It needs to have a remote
  3. You must have pushed to the remote at least once.
  4. You also need to make sure your working directory is clean.
# Initialize Git git init # Adds a remote repository git remote add origin some-url # Commit changes git add . git commit -m "Initial Commit"

If your project is not a Git repository, you’ll get this error:

If your project doesn’t have remote, you’ll get this error (at a later part of the checks).

If your working directory is dirty, you’ll get this error:

If you haven’t pushed to the Git remote at least once, will just hang and do nothing.

Using npm

To use , you run the command.

np

will prompt you to enter a Semver number.

Choose a number and will ask you to confirm your choice.

then does the rest of the publishing stuff for you.

Error with running tests

runs the command as part of its checks.

If you followed the tutorial up to this point, you would get an error that looks like this:

This happens because our command results in an error. You can try it yourself:

npm test

To fix this error, we need to change the script in file.

Right now it looks like this:

"scripts": { 
"test": "echo \"Error: no test specified\" && exit 1"
},

Change it to this:

"scripts": { 
"test": "echo \"No test specified\""
},

This change works because creates an error.

With this change, should complete the publishing process. (Remember to commit the change before running ).

At the end of the process, launches a browser window for you to write your release notes.

In short, makes publishing packages much simpler!

Thanks for reading. Did this article help you out? If it did, I hope you consider sharing it. You might help someone else out. Thanks so much!


This article was originally posted at my blog.
Sign up for my newsletter if you want more articles to help you become a better frontend developer.

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Zell Liew

Written by

Zell Liew

Frontend consultant; teaching frontend to self-taught devs at zellwk.com. Currently attempting to demystify Javascript at https://learnjavascript.today

freeCodeCamp.org

This is no longer updated. Go to https://freecodecamp.org/news instead

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade