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:
Follow these steps:
β 1. Create Your Project Folder
Example:
Go inside it:
β 2. Initialize package.json
Run:
Or fast version:
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:
β 5. Login to NPM
If you donβt have an account β create:
π https://www.npmjs.com/signup
Login via terminal:
It will ask:
username
password
email
β 6. Publish the Package
Run:
π Congratulations! Your package is now live.
Users can install it:
π 7. Publishing Scoped Packages (Optional)
If your username is vipul, scoped package example:
Publish:
π 8. Updating a Published Package
You must change the version each time you publish again:
Inside package.json:
Then:
β 9. Unpublishing a Package
Within 24 hours:
β Be careful: Unpublishing affects users.
π¦ Folder Structure Example
β Optional (but Highly Recommended)
Add README.md
This appears on your NPM package page.
Add .gitignore
Especially for node_modules:
π― Summary
| Step | Action |
|---|---|
| 1 | Create folder |
| 2 | npm init |
| 3 | Write your code |
| 4 | Update package.json |
| 5 | npm login |
| 6 | npm publish |
| 7 | Update version before republish |
| 8 | npm unpublish (careful!) |
