Publishing a Node.js Module to NPM

Introduction

NPM stands for Node Package Manager and refers to either the online project repository or the Command Line Interface (CLI) tool used to interact with the online repository.

Currently, NPM is the world's largest software registry with over 1.4M+ code packages. For example, if you wanted to use Node.js to send emails - you could do a quick search on their repository and find a module like Nodemailer, which can save you a lot of time.

You might want to share a tool you've created with the community, that streamlines some boilerplate task. In this article, we'll take a look at how to publish a Node.js module to NPM.

Getting Started with NPM

To publish a module to NPM, we need to have an NPM account. If you don't have one, you can create it here.

When you install Node.js, the npm tool is automatically installed as well.

To check whether npm has been installed properly, you can check what the currently installed version is by running:

$ npm -v

Once ascertained that we've indeed got npm installed, we can log in:

$ npm login

The tool will prompt us for our id, email and password created while signing up.

Note: You don't need to have an account to use NPM, but to publish a module, you'll need one as it's tied to you as the author.

Creating a Package

Let's create a directory for our project and move into it:

$ mkdir publish-to-npm
$ cd publish-to-npm

Next, we'll initialize the project:

$ npm init 

The command will then prompt you with several questions that, when filled in, produce a package.json file:

{
  "name": "publish-to-npm",
  "version": "1.0.0",
  "description": "\"Sample module for publishing\"",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "[email protected]",
  "license": "ISC"
}

The main entry point, by default, is the index.js, so we'll want to create that file and add something to it.

If you used the -y flag alongside the init command, these would've been set to their defaults.

Adding Dependencies

Let's add some code to our index.js file and add a dependency, such as another NPM module for it to use.

First, we'll install a dependency:

$ npm install node-fetch --save

Then, we'll import it to our index.js file and add some logic:

const fetch = require('node-fetch');

fetch('https://google.com')
    .then(res => res.text())
    .then(text => console.log(text))

To verify this is working, we can run:

$ node index.js
Free eBook: Git Essentials

Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!

And the output is:

<!doctype html>
<html itemscope="" itemtype="http://schema.org/WebPage" lang="en-RS">
    <head>
        <meta charset="UTF-8">
        <meta content="origin" name="referrer">
        <!-- Rest of the page -->

When checking the package.json file now, we can see node-fetch in our dependencies:

{
  "name": "publish-to-npm",
  "version": "1.0.0",
  "description": "\"Sample module for publishing\"",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "[email protected]",
  "license": "ISC"
  "dependencies": {
    "node-fetch": "2.6.1"
  }
}

Note: The --save flag tells npm to update the project with the module as a dependency. This has been lifted in the newer versions and the module will be added to the dependency list even without the --save flag, though, for older versions, you'll have to use it.

Publishing to NPM

Now, we can publish the module to NPM via the publish command:

$ npm publish

If you get any error, it's most probably because of a name collision. i.e., a package with the same name already exists on npm. We have to change the name of our project to something unique.

You can search for existing package names through the search bar on the home page.

After you come up with a unique name, you have to change the package name in our package.json file. For the sake of consistency, it's also advised to update the folder name to match the package name.

If you are not able to come up with a unique name, you should check out scoped packages. This essentially gives you your own namespace on NPM, so you can name your packages whatever you want.

It's generally advised that, if you plan to make a module public, you still avoid using the same name as another package to avoid unnecessary confusion. However, it's very practical for personal use.

This is one of those settings we avoided changing by using the -y flag. If you wanted to make a scoped module, you would need to initialize your project with:

$ npm init --scope=@your-username

Conclusion

In this article, we've written up a simple app and published it to NPM as a public module for other developers to use at their convenience.

You might come across a situation in the future where you don’t find a module readily available to solve your particular problem. You can write the solution yourself and upload it to NPM, and maybe help out other developers with the same problem.

Last Updated: September 18th, 2023
Was this article helpful?

Improve your dev skills!

Get tutorials, guides, and dev jobs in your inbox.

No spam ever. Unsubscribe at any time. Read our Privacy Policy.

Abhilash KakumanuAuthor

Hey, I am a full-stack web developer located in India. I am a curious person who is always trying to wrap my head around new technologies. In my free time, I read novels and play with my dog!

© 2013-2024 Stack Abuse. All rights reserved.

AboutDisclosurePrivacyTerms