Node.js Publish a Package

πŸš€ Node.js – Publish a Package to NPM

Publishing your own package on NPM allows developers around the world to install and use it with:

npm install your-package-name

Follow these steps:


βœ… 1. Create Your Project Folder

Example:

my-awesome-package

Go inside it:

cd my-awesome-package

βœ… 2. Initialize package.json

Run:

npm init

Or fast version:

npm init -y

This creates a basic package.json.


βœ… 3. Create Your Main File

Create index.js:


 


βœ… 4. Update package.json (Important)

Open package.json and add:

⚠ Requirements for package name:

  • must be unique

  • must be lowercase

  • cannot contain spaces

  • can use hyphens -

To check availability:

npm search your-package-name

βœ… 5. Login to NPM

If you don’t have an account β†’ create:

πŸ‘‰ https://www.npmjs.com/signup

Login via terminal:

npm login

It will ask:

  • username

  • password

  • email


βœ… 6. Publish the Package

Run:

npm publish

πŸŽ‰ Congratulations! Your package is now live.

Users can install it:

npm install my-awesome-package

πŸ”’ 7. Publishing Scoped Packages (Optional)

If your username is vipul, scoped package example:

Publish:

npm publish --access public

πŸ” 8. Updating a Published Package

You must change the version each time you publish again:

Inside package.json:

"version": "1.0.1"

Then:

npm publish

❌ 9. Unpublishing a Package

Within 24 hours:

npm unpublish my-awesome-package --force

⚠ Be careful: Unpublishing affects users.


πŸ“¦ Folder Structure Example

my-awesome-package/
β”‚
β”œβ”€β”€ index.js
β”œβ”€β”€ package.json
└── README.md (recommended)

⭐ Optional (but Highly Recommended)

Add README.md

This appears on your NPM package page.

Add .gitignore

Especially for node_modules:

node_modules/

🎯 Summary

StepAction
1Create folder
2npm init
3Write your code
4Update package.json
5npm login
6npm publish
7Update version before republish
8npm unpublish (careful!)

You may also like...