Build and publish your first NPM package

Ogunniyi Tunmise
Nov 30, 2018 · 4 min read
NPM; NodeJS Package Manager.

What is NPM

NPM is a package manager for NodeJS. It was created in 2009 as an open source project to help javascript developers easily share code in form of packages. Since then thousands of packages have been published to the NPM registry where all the packages are hosted. This article will walk you through how you can create and publish your own NPM package.

What are we building/publishing

The focus of this article is not to build a bad-ass NPM package but instead to explain how to build and publish an NPM package. The build and publish cycle is nearly the same for any other package, once you understand how its done for something as simple as what we will be building in this article, you are good to go.
In this article, we will be building a random number generator, very simple hun… That’s the idea.

Building the package


All you need to build(and then publish) an NPM package is the NPM command line tool which also goes by the name npm. npm is distributed with NodeJS, this means that when you install NodeJS, you automatically get npminstalled on your computer. Visit here to download NodeJS.
To verify that npm was installed properly, you can run npm --version, it should output a version.

The single and most important file

After downloading npm, we can go ahead to start creating our package. Due to the simplistic nature of the package, most of the coding would be done in the command line interface(CLI). In the CLI do the following:

# Create the project directory
mkdir random-number-generator

The npm init command should reveal an interactive session in the CLI that looks like this:

npm init interactive session

You will be required to answer some questions, these questions are basically about the NPM package you want to create. The command line tool auto-suggests reasonable answers to the questions, if you feel the suggested answer is good enough, just hit enter. If you also don’t have an answer to any of the question, hit enter and continue, you can always edit it later.

After answering the questions, a package.json file will be created in the root of the project folder, the content will be similar to this:

"name": "random-number-generator",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"author": "",
"license": "ISC"

Package.json is the single and most important file as far as creating/publishing an NPM package is concerned, without it, you won’t be able to publish whatever you create to the NPM registry.

Note the main field in the package.json file, it refers to the name of the file that would be loaded when your package is required by another application, by default it points toindex.js

Adding the project codes

The project to be created is a very simple one, in fact, all the project code will be composed in a single file. Feel free to create something more complex.
Create an index.js file in the root of the project folder then add the code for the random number generator:

function randomNoGenerator(min, max) {
if(typeof(max) !== 'number' && typeof(min) !== 'number') {
min = 0; max = 1;
return (Math.random() * (max-min)) + min;

Notice the module.exportsat the bottom of the file, whatever you are exporting here is what would be available for importing when others install the package.

Publishing the package


You need to create an account in the NPM registry, you can do this here. If you are having any issues creating an account check the documentation here.

After creating an account, go back to the command line and authenticate yourself with the command npm login, you would be prompted to provide your details, provide the required details and hit enter.
To test that the login was successful, enter the command npm whoami, your username should be logged to the CLI.


Note that you may not be able to publish the random-number-package if someone else already has a package with the same name in the registry. You can simply change the name of the package to something unique to make it publishable. Check here for guides on naming a package.

Publishing your NPM package is as simple as entering this command in the CLI:

npm publish

After the publish is done without any error, you can visit your account in the NPM registry to see the package.


To test the package, you simply need to install and use it. You can do the following to test the random-number-generator package:

# Create a test directory
mkdir random-number-test

# Change into the directory
cd random-number-test

Copy this simple test code and paste it in the test.jsfile.

const randomNumberGenerator = require('random-number-generator');

Execute the code in the test.js file using node ./test.js, you should see a random number logged to the console.

Further reading

Adding a README to the project on NPM: Link

Versioning of NPM packages: Link

Private NPM packages: Link

The Andela Way

A pool of thoughts from the brilliant people at Andela

Thanks to John Kagga

Ogunniyi Tunmise

Written by

Passionate about moving from ideas to reality. Software dev at Andela.

The Andela Way

A pool of thoughts from the brilliant people at Andela

More From Medium

More from The Andela Way

More from The Andela Way

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