How to Publish an npm Package: Step‑by‑Step Guide for Node Developers

This guide walks you through installing Node.js, creating an npm account, initializing a Git repository, setting up package metadata with npm init, testing locally, logging in, and finally publishing or removing a package on the npm registry, complete with command examples and best‑practice tips.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How to Publish an npm Package: Step‑by‑Step Guide for Node Developers

npm (Node Package Manager) is an online repository and CLI tool for publishing and managing Node.js packages.

Step 1: Install Node.js

Install Node.js (which includes npm) globally on your system (Ubuntu, Windows, etc.) following the official Node.js website instructions.

Step 2: Create an npm Account

Register an account on the npm website. Fill in details, accept terms, and verify the email link sent by npm; otherwise publishing will fail.

Step 3: Initialise a Git Repository

In the project root, run: git init Create a .gitignore file, then add all files and commit the first snapshot:

git add -A
git commit -m "initial-commit"

You may host the repository on GitHub, GitLab, etc., for collaboration.

Step 4: Initialise npm in the Project

Run the following command in the project root to generate package.json: npm init The CLI will prompt for several fields:

package name : unique name for the npm package.

version : current version, default 1.0.0; must be incremented on updates.

description : public description of the package.

entry point : file that starts execution.

test command : command run by npm test.

git repository : URL of the remote Git repository.

keywords : tags to improve discoverability.

author : your name or alias.

license : default ISC license.

After filling these, the package skeleton is ready.

Step 5: Test Your Package Locally

Link the package locally: npm link Create a separate test folder, link the package, and create a test file to verify functionality. Remove the test folder after verification.

Step 6: Log In to npm

Authenticate your CLI with your npm account: npm login Enter username, password, and optional two‑factor code.

Step 7: Publish the Package

Run: npm publish You will be prompted for a one‑time password sent to your email. After publishing, the package appears on npmjs.com under your profile.

Removing a Package

You can delete or unpublish a package from its settings page on npm if needed.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Node.jsnpmpackage publishing
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.