Contact Us

Packaging Node.js Applications with NPM

Learn how to create npm packages for your node.js apps

What's inside this article?
This article is about packaging Nodejs applications using the node package manager or npm . We'll begin with an answer to what is npm and some basic npm commands and by the end of the article we'll learn to create our own npm package and see how to publish it to the npm repository for others to use it freely. In the process, we'll also learn to create our npm account in case we don't have it already.
Audience for this tutorial includes those looking to learn about packaging nodejs apps. It is written to be more beginner friendly but it is sure to help advanced and more experienced developers also.
Pre-requisites: you'll need node & npm in order to run commands used in this tutorial. Apart from these tools a basic working knowledge of nodejs and npm is assumed.

Did you know? that npm is the largest ecosystem of open source libraries.

What is NPM?
NPM or Node Package Manager is a package manager for Nodejs applications which means that it helps you to create, manage, publish and install Nodejs packages. NPM makes it very easy for Nodejs developers to do the following:
  1. Sharing Code with other developers by publishing packages
  2. Reusing Code published by other developers
  3. Managing Dependencies by automatically installing/updating/deleting packages as required
  4. Deploying code by making it easier to manage the complete application
Basic NPM Commands
Following are some of the very basic npm commands. Read the comments which explains what the command intends to do.

	npm -v // prints the version of npm installed
	npm install [package-name] // install a package. See example in next line.
	npm install request 	   // install a package "request" locally
	npm install request -g 	   // install a package globally
	npm update [package-name]  // update the given package
	npm uninstall [package-name] // uninstall the given package
Mentioned above are only a few basic npm commands for a more comprehensive and complete list of npm commands, vist the NPM CLI Commands page
Creating an NPM Package
Open your terminal or cmd and change to the directory where your would like to create your npm package and run the following command.

		npm init
Execution of above command will open an interactive session with a few simple questions like:
  1. name : give a name for your package
  2. version : give a version number or hit ENTER to keep the default value
  3. description : give a brief description about your package.
For many of these questions, you can use default values. Once you've answered all these questions, you'll be shown all the values you've entered and all you need to do is type "yes" and hit enter once again.
Congratulations! you've created your npm package. You can verify this by using 'ls' or 'dir' commands to see if the package.json file has been created.
Add a Code file to Package
We've created our package, but it has nothing except for the package.json file. Let's add a code file to our package. Write the following code in a file and save it as index.js in the same directory as your package.json file.

// index.js
"use strict"

class Ganit{
	constructor(x,y) {
		this.x = x;
		this.y = y;
		return this.x + this.y;
		return this.x - this.y;
		return this.x * this.y;
		return this.x / this.y
		return this.x % this.y
module.exports = Ganit
Publish your Package to npm registry
Now that we've created our package and also added some code to it. We're ready to let other make use of our package by publishing it to npm.
If you don't already have an npm account, you should create your npm account now by clicking the button below.

Now go back to the directory where you created your package and execute following npm command and provide your credentials which you used above to create your account.

	npm login
Once you're logged in, run the following command to publish your first package.

	npm publish
If everything goes well, the above command will publish your package to npm. You can verify that by opening your npm account in the browser. Now we can do one last thing i.e. installing our own module. Just use the following command.

	npm install YOUR_PACKAGE_NAME
And finally, use your node module in a file by using "require".

	var MyModule = require('Your_Package_Name');
That's it! This is how you create and publish your nodejs modules to npm. In case you face problems we suggest you the following resources.
Links to Useful Resouces

More from RISHABH.IO

Image Credits:

Icons made by Gregor Cresnar from www.flaticon.com is licensed by CC 3.0 BY

developed & nourished with by rishabh.io