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
Step 2: Create Your Project
- Make a new folder for your package.
mkdir my-package
cd my-package
- Initialize the package:
npm init -y
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;
Update package.json
to set the entry point:
"main": "index.js"
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
Step 5: Publish the Package
Run the command:
npm publish
By default, the package will be public. Anyone can install it using:
npm install my-package
Step 6: Update the Package
If you make changes, bump the version in package.json
(for example 1.0.1
→ 1.0.2
) and republish:
npm version patch
npm publish
Top comments (0)