It is also a command-line tool that lets you interact with registered packages. It can help with package installation, version management, and dependency management.

Developers publish their JavaScript libraries on npm every day. The process is fairly simple, so you too can publish a package on npm.

Step 1: Installing NodeJS

To interact with npm, you must have it globally installed on your system.

Start by installing NodeJS and npm on Ubuntu, on Windows, or by following the instructions on the NodeJS website.

Step 2: Creating an npm Account

To publish your packages on npm, you must create an npm account on the official npm website.

Follow the steps below to create an npm account:

Go to the npm signup page. Fill in your details, agree to the license and privacy policy, and click on “create account”. Verify your email with the link you will receive from npm in your mailbox.

Step 3: Initializing a Git Repository

Next, you need to initialize a Git repository to track all the changes you’ll make to your project.

On your terminal, navigate to your project’s root directory and run the following command to initialize an empty Git repository.

Create a .gitignore file and add any files that you don’t want Git to track. Then run the following command to track the remaining files:

Next, run the following command to take your first snapshot by committing the code to Git.

You can choose to host your Git repository on GitHub or a similar service like Gitlab for seamless collaboration.

Step 4: Initializing npm in Your Project

Next, initialize npm in your project to add details about your project to a package.json file. These details will be public on npm.

On your terminal, navigate to your project’s root directory and run the following command to initialize npm in your project.

The command above will trigger a set of prompts on your command line:

package name: This is the name of your npm package; it must be unique if you want to publish the package on npm; else, npm will throw an error. version: This property denotes the current version of your package. You must increment it every time you update your package or npm will throw an error. The default value is 1. 0. 0. description: This property is the description of your package. It will be public when you publish your package. entry point: This property denotes the file name where your code execution starts. test command: This property indicates the command that will run when you execute npm run test. git repository: This property is the URL for your remote Git repository. keywords: This property denotes the relevant keywords to your project that will help the search engine to find it. author: This property should be your name or alias. license: This is the license that you want to publish the package under. The default license is the Internet Systems Consortium (ISC) license.

After setting this up, you can create your package.

Step 5: Testing Your Package

After you have finished creating your package, you should test it locally to see how it would behave after you’ve deployed it on npm.

To test your package locally, first, run the following command in your project’s directory:

The command above allows you to reference your package locally as you would an npm-hosted package.

Then, create another folder in your project’s root directory and cd into it.

In the test folder, run the following command to link your package to the folder:

Finally, create a dummy file to test your package’s functionality.

After completing your tests, you can safely delete your test folder.

Step 6: Logging In to npm on Your System

Next, you have to log into your npm account on your system.

Run the following command to log into your npm account:

This will prompt you to fill in your username, password, and a two-factor authentication code (if you enabled it on your account).

Step 7: Publishing Your Package

After you have completed all the steps above, run the following command to publish your package on npm.

This command requires a one-time password that will be to your email. Then, it will publish your package on npm.

You can view the packages you have published on npm. Log in to your npm account on npmjs.com and navigate to the packages section on your profile dashboard.

To install your package and use it as a dependency on any of your projects, run:

Deleting Your Package From npm

You still have complete control over your packages on npm, meaning you can modify and even delete your package permanently from npm. You can delete your packages by going to the settings section of your package and selecting the delete package option.