Publish Node package on NPM

Asad Iqbal
2 min readOct 30, 2020
npmjs.org

NPM registry is the best place to share your node package globally. NPM is powered by a couchDB database. We will go through the process of creating the node package and publishing it as a public package.

  1. First you have to create a free account on https://npmjs.org.

2. Login the npm cli using your username and credentials.

On the command line -

npm login
Username: <your-username>
Password:<password>
Email: (this IS public) <youremail>@<domain.com>

If the above information is correct you will get the message as:

Logged in as <username> on https://registry.npmjs.org/.

3. Now we have to create a node package, you need a directory to hold your code and initiate the package creation.

On the command line -

mkdir mynodepackage

cd mynodepackage

Now we are in the directory of our project, here we will initiate the npm package using command :

npm init <mynodepackage>

package name: (mynodepackage) mynodepackage
version: (1.0.0)
description:
entry point: (index.js)
test command:
git repository:
keywords:
author: asad.iqbal
license: (ISC) MIT
About to write to C:\mynodepackage\package.json:

{
“name”: “mynodepackage”,
“version”: “1.0.0”,
“description”: “”,
“main”: “index.js”,
“scripts”: {
“test”: “echo \”Error: no test specified\” && exit 1"
},
“author”: “asad.iqbal”,
“license”: “MIT”
}

4. Great you have your package ready to publish, though it is an empty package, if you want to export modules then add them in index.js which is the entry point of the package.

5. Lets publish it on npm now

On command line:

npm publish — access public

If the name of your package is not unique accross whole registry then you will get the following error -

403 Forbidden — PUT https://registry.npmjs.org/qdb — You do not have permission to publish “qdb”. Are you logged in as the correct user?

In this case you can append some text with your package name to make it unique and try publishing again.

Great your NPM package is now published, and you know so many developers had been waiting to install your package and use your awsome code!!!.

They just need to write

npm install <your-package-name>

WALLA !!!!

--

--