How to publish apps to NPM

Overview

NPM is a free registry for public package authors. We can use it to publish any project (folder) from our computer that has a package.json file.

Below are the steps required to share your package with the world:

Step 1: sign-in or sign-up

Go to the NPM website and sign in (or sign up if you do not yet have an account).

Note: Make sure to verify your email after creating a new account. Otherwise, you will get a 403 Forbidden error while publishing your package.

Step 2: log in

Log in to your NPM account from the command line using the command below:

npm login

Note: You can use the npm whoami command to check if you are currently logged in.

Step 3: publish the package

Go to your project's root directory and publish it using the command below:

npm publish

Make sure your package's name does not currently exist on NPM. Otherwise, you will get an error while publishing. You can use the npm search command (or the NPM website's search bar) to search if the name you wish to use already exists on NPM.

Suppose all the suitable names for your package are already taken. In that case, NPM allows you to publish your project as a scope. In other words, you can publish your package as a sub-section of your username. Let's see how below.

How to publish your package as a scope of your username

Open your package.json file and prefix your package's name with your username.

Example

{
"name": "@username/package-name",
"version": "1.0.0",
"main": "index.js",
"license": "MIT"
}

NPM's default setting assumes that a scoped name package is a private project. So, you will get an error if you use the npm publish command to share a scoped name package.

Therefore, to publish your package as a scope of your username, add the --access=public flag to the npm publish command:

npm publish --access=public

Note: You can make your project a scoped package during the initialization process by using the npm init --scope=username command instead of npm init.

Free Resources

Attributions:
  1. undefined by undefined
  2. undefined by undefined