As a developer, there is no greater feeling than being able to see our work being used by others. One way you can do that is by creating packages for Node.js and releasing them on NPM, which is the world's largest software registry. Some of the top packages on NPM have millions of downloads per week!

NPM download stats
NPM download stats

Create your package

In this tutorial, we'll create a simple NPM package that says hello to our users. Create a directory for your package and create a new file index.js in the directory.

$ mkdir hi-hello-pkg

To use the package, the users will have to call a function that we expose in the package. We will export a function named hello.

exports.hello = async function hello() {
    console.log("Hello! You look nice today.");
}

To make this into a package, we need a package.json file which we can create by calling npm init on the package directory. It will ask you a series of questions like package name, description, version etc. that you can answer to create the package.json file. Pick a unique name for the package, one that has not belong to another package on the npm registry.

{
      "dependencies": {},
      "name": "hi-hello",
      "version": "1.0.0",
      "description": "Just saying hello!",
      "main": "index.js",
      "devDependencies": {},
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [
        "hello",
        "hi"
      ],
      "author": "Pru",
      "license": "ISC"
}

After the package.json file is created, your package is technically ready to publish. To make it more user friendly, you should add a README.md file to the package directory. This file is in Markdown format and its contents will be displayed by NPM on your package page.

#### Hi Hello!
A nice NPM package that says Hello to you! 

#### Installation
* Install the **Hi Hello** package for Node.js

        npm install hi-hello

#### Usage

        const hiHello = require('hi-hello');
        
        // Say Hello!
        hiHello.hello();

#### Output
        Hello! You look nice today.

Test your package

Before you publish the package to NPM, you should test it locally. We can do this by installing the package from the local directory which contains our package files.

$ npm install ./hi-hello-pkg

Now create another file called test.js and include your local package and test it. If it works, your package is ready to publish.

const testHello = require('hi-hello');
testHello.hello();

Publish package to NPM

Create an account on NPM. Then login to the account from your terminal.

$ npm login

After logging in, go to your package directory and publish.

$ npm publish

If everything works, then the package will be published to NPM and you will receive and email from NPM about it. You can go to the package page on NPM page and check it out.

At this point, you should install your package from NPM registry and test that it works correctly.

$ npm install hi-hello

Update package

Whenever you want to change the package, you should make sure to update the version in the package.json file before publishing the package again.

Conclusion

That's it! You have now published your very own package to NPM where millions of other developers can now access it.