How to create an NPM package

In this shot, we will learn to create a private npm package that can be published.

Step 1

If you are using npmrc, switch to an appropriate user profile. On the command line, type the following command:

npmrc <profile-name>
// profile-name refers to the user profile name. Replace it with your profile name.

Step 2

On the command line, type the following command to create a directory for your created package:

mkdir my-test-package

Step 3

Navigate to your package’s root directory by typing the command below:

cd my-test-package

Step 4

If you are using git to manage your package code, run the following commands:

git init
git remote add origin git://git-remote-url

Remember to replace git-remote-url with the git remote URL for your package.

Step 5

Run npm init in the package root directory and pass the scope to the scope flag.

If the package is scoped within an organization, run the following command and replace my-org with the name of your organization:

npm init --scope=@my-org

For a user-scoped package, replace my-username with your username:

npm init --scope=@my-username

Step 6

Respond to the prompts to generate a package.json file.

Step 7

Although it is optional, it’s a good practice to create a README file that explains how to use your package.

Step 8

Open a text editor of your choice and write the code for your package.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved