DEV Community

Safal Bhandari
Safal Bhandari

Posted on

How to Publish Your Package on npm

The npm (Node Package Manager) registry lets developers share reusable code packages with the community. If you’ve created a library, tool, or utility in JavaScript or TypeScript, you can publish it to npm so others can install it with a simple command.

Step 1: Install Node.js and npm

  • Download and install Node.js from nodejs.org.
  • npm comes bundled with Node.js, so no extra installation is needed.
  • Verify installation:
  node -v
  npm -v
Enter fullscreen mode Exit fullscreen mode

Step 2: Create Your Project

  1. Make a new folder for your package.
   mkdir my-package
   cd my-package
Enter fullscreen mode Exit fullscreen mode
  1. Initialize the package:
   npm init -y
Enter fullscreen mode Exit fullscreen mode

This generates a package.json file with basic metadata.

Step 3: Write Your Code

Example index.js:

function greet(name) {
  return `Hello, ${name}!`;
}

module.exports = greet;
Enter fullscreen mode Exit fullscreen mode

Update package.json to set the entry point:

"main": "index.js"
Enter fullscreen mode Exit fullscreen mode

Step 4: Log in to npm

If you don’t already have an npm account, create one at npmjs.com.
Then log in through the terminal:

npm login
Enter fullscreen mode Exit fullscreen mode

Step 5: Publish the Package

Run the command:

npm publish
Enter fullscreen mode Exit fullscreen mode

By default, the package will be public. Anyone can install it using:

npm install my-package
Enter fullscreen mode Exit fullscreen mode

Step 6: Update the Package

If you make changes, bump the version in package.json (for example 1.0.11.0.2) and republish:

npm version patch
npm publish
Enter fullscreen mode Exit fullscreen mode

Top comments (0)