How to publish your package on NPM

How to publish your own package

Several developers feel the need to use some functionality of one project in another. Normally, the developer copies code from one project and pastes in another but if it is common functionality that may be used in several projects its a better and good practice to publish your reusable codes as npm packages.

Okay, so let’s start!

  1. npm init:

When you are in your desired directory, open the CLI, and use this command:

npm init

A text utility appears and you are asked to enter a few details to create a basic package.json file such as package name, version, description, author, etc.

Enter the details of the package as you please. The defaults will be mentioned in parentheses like this →

package name: (test-package)

version: (1.0.0)

description: This is a test package




Just hit Enter if you want the default options.

A basic package.json will be created looking something like this :


"name": "test-package",

"version": "1.0.0",

"description": "This is a test package",

"main": "app.js",

"author": "ashramtech",

"license": "ISC"


Obviously, you can change the package.json file later if you want.

  1. Source

Now, you need to prepare the source code. If the code is not too big, it is typically just write in the main file(app.js here). Else, conventionally a src directory is used where your abstract code is stored in several files.

Remember to export the code using module.exports

  1. Test

You now need to thoroughly test your code before publishing. This is how you, as a developer, confirm that your package can actually be used.

  1. Publish

If this is your first time, you probably won’t be logged in with your npm account. If you don’t have an account, go to and create your account.

Now, in your terminal, log in with your npm account.

npm login

And now to publish your package,

npm publish

And you’re done!

You can now check your published project at<username>/<pkg-name>.

The details regarding the package such as version, author name, description, etc. are extracted from the package.json file you created earlier.


Now you’ve got a basic idea about what npm is and how you can publish your own package to the npm registry. If you want to know more about npm and its basic commands please follow this article An Introduction to the npm : An Absolute Beginner’s Guide