How to publish a public module on NPM in 3 minutes?

NicolasBrondinBernard

Author
@NicolasBrondinBernard

Want to make your open-source project available to node-packet manager users? Here's how to do it!

Article published on 31/01/2021, last updated on 10/08/2026

A few days ago, I told you about my open-source project Peek, a utility that automates screenshots of web projects. I even dedicated an entire article to it, available here.

But since yesterday, it's available directly from npm as a public package on NPM, right here!

You can therefore install it globally at any time locally by simply running:

$ npm install -g peek-js

And to celebrate the publication of my first module, I thought a quick and detailed tutorial might help some of you, so here it is!

Step 1: Create an account

As you know, it's possible to use the NPM registry without having to create an account, simply to install new modules or manage your project's dependencies.

I actually published an article titled "7 NPM commands you've never heard of" to make things easier for you!

But to be able to publish new packages yourself, you need to create an account on the platform: https://www.npmjs.com

Once the account is created with your email address, username, and password, all you need to do is verify your email address to complete the process (don't forget this).

Step 2: Log in

The platform might lead you to think that creating new modules is done directly from the web interface, but that's not the case at all.

The rest of this tutorial takes place entirely from your terminal.

To log in with the account you just created, open your favorite terminal, and enter the following command:

$ npm login

After this, NPM will ask for your login information as you created it during registration. Enter it, and NPM should display the following message: "Logged in as [username] on https://registry.npmjs.org/"

Step 3: Generate the manifest

If you're already using NPM to manage your project's dependencies, you've probably already generated the manifest needed to publish your package, and you can skip this step.

If you're not sure you have all the necessary information in the file, I invite you to check the package.json documentation!

If your project doesn't yet contain a manifest, NPM provides a very simple utility to generate one from your project's basic information. To use it, simply run the following command:

$ cd [project-folder]
$ npm init

And enter the requested information as shown below:

package-name:
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords: 
license: (MIT)

The essential information is the package name and its version. Note that the package name must not contain spaces, and the version must follow the "semantic versioning" format.

Step 4: Publish your package

Once the manifest is created, publishing your module only requires a single command, provided by NPM:

$ npm publish

Warning, your package name must be unique and the NPM registry is case-sensitive. If your package conflicts with an already existing package, your terminal will return the following error:

403 Forbidden - PUT https://registry.npmjs.org/[package] - You do not have permission to publish "[package]". Are you logged in as the correct user?

All you need to do is change the name of your module in the manifest, and try again!

It's best to check beforehand on the website whether the name is available, or by using the command "npm search [package]".

If everything goes well, you should just see one additional line after the NPM logs indicating: "+[package]@[version]". Congratulations!

Step 5: Test

The advantage of the NPM registry is that it updates instantly, so all you need to do is install your own module to see if it was correctly added to the registry. For this, nothing could be simpler:

$ npm install [package]

And there you go, your first public NPM package is online! Note that you can also publish private modules (to make managing dependencies within your team easier), but hosting isn't free, unlike the public registry!


AbsolutVision sur Unsplash

Finished reading this article?
Our complete courses
Take it to the next level with our courses!

Complete courses, exercises and certificates to really learn programming!

4.8 average rating

Comments (0)

to leave a comment

No comments yet