Learn Programming, Tech & Coding · Free Online Tools

IT Question Answer
Back to How To Guides
How to Publish an NPM Package

How to Publish an NPM Package

How To Guides610 viewsBy Admin
how-topublishpackage

Advertisement

Publishing to NPM

Share your code with the world by publishing it to the npm registry. Here's the complete process.

Step 1: Create & Login

npm login        # log into your npm account
# (create one free at npmjs.com first)

Step 2: Set Up package.json

{
  "name": "my-unique-package",
  "version": "1.0.0",
  "main": "index.js",
  "description": "What it does",
  "keywords": ["utility"],
  "license": "MIT"
}

Step 3: Write Your Code

// index.js
module.exports = function greet(name) {
  return `Hello, ${name}!`;
};

Step 4: Publish

npm publish
# For scoped public packages:
npm publish --access public

Step 5: Update Versions

npm version patch   # 1.0.0 → 1.0.1
npm version minor   # 1.0.0 → 1.1.0
npm version major   # 1.0.0 → 2.0.0
npm publish

Important Tips

  • The package name must be unique on npm.
  • Add a README — it becomes your package page.
  • Use .npmignore to exclude files.

FAQs

Can I unpublish?

Within 72 hours, yes. After that it's restricted to avoid breaking dependents. More in our how-to guides.

Is publishing free?

Yes for public packages.

Advertisement