Oct 04, 2024

How to Publish Package to NPM – Step by Step

Prerequisite

Make sure you have an NPM account. If not, just go to NPM and create a free account.

You need to have an IDE, either VS Code or IntelliJ.

Step-by-Step Guide

1. Set up your project

Start by initializing a new project. Run this command in your terminal:

npm init

Follow the prompts to create a package.json file for your project.

2. Write your code
Create your code and make sure it works properly. Your main entry file (e.g., index.js) should be set in package.json under "main".

3. Add a .gitignore file
Include files and directories you don’t want to publish (e.g., node_modules) in a .gitignore file.

4. Log in to NPM
Run the following command to log in to your NPM account:

npm login

If you don’t have an account yet, sign up at npmjs.com.

5. Publish your package
Once everything is set, publish your package by running:

npm publish

Make sure your package name is unique. If it's scoped (e.g., @username/package-name), use npm publish --access public to make it public.

6. Update your package

For future updates, simply bump the version in your package.json and run:

npm publish

Conclusion

Publishing an NPM package is as simple as initializing a project, writing your code, and running a few commands. Now your package is live and available for the world to use!

OTHER POSTS

Use npm scripts to Simplify Daily Tasks

Running long terminal commands over and over? Automate them with simple npm scripts in your package.json.

Read more →

Learn Faster with Browser DevTools Tricks

Your browser is more powerful than you think. Chrome/Edge/Firefox DevTools can save you tons of time debugging and experimenting directly in the browser.

Read more →

Fix "Email in this signature doesn't match the committer email" on GitHub

Pushed a signed GPG commit only to get a warning that the signature is unverified because of an email mismatch? Here is how to sync your GPG identity with Git.

Read more →

How to Set Up Verified GPG Commit Signing on GitHub

Tired of seeing the "Unverified" badge on your GitHub commits? Setting up a GPG key signs your commits cryptographically, proving to your team (and future employers) that the code actually came from you.

Read more →