
How to Publish Your Library to npm
How to Publish Your Library to npm 관련
Now that we’ve created our npm package, the next step is publishing it to the npm registry so others can install and use it. In this section, we’ll cover how to publish the package step by step.
Creating an npm Account
Before publishing, you need an npm account.
Step 1: Sign Up for npm
- Go to https://npmjs.com/signup and create an account.
- Verify your email address.
Step 2: Log in to npm from the Terminal
Run the following command in your terminal:
npm login
You will be prompted to enter:
- Your npm username
- Your password
- Your email (associated with your npm account)
If the login is successful, you’ll see a message:
Logged in as your-username on https://registry.npmjs.org/
Configuring package.json for Publishing
Step 3: Ensure Your Package Name is Unique
Every npm package needs a unique name. Run the following command to check if your desired name is available:
npm search my-awesome-library
If the name is already taken, you’ll need to modify package.json
and change the "name"
field.
Step 4: Add Metadata and Keywords
Open package.json
and ensure it includes useful metadata:
{
"name": "my-awesome-library",
"version": "1.0.0",
"description": "A simple npm package for formatting dates",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/yourusername/my-awesome-library.git"
},
"keywords": ["date", "formatter", "utility", "npm package"],
"author": "Your Name <your-email@example.com>",
"license": "MIT"
}
🔹 repository – Useful if you plan to host the project on GitHub.
🔹 keywords – Helps people discover your package on npm.
🔹 license – Specifies how others can use your package (for example, MIT, GPL, and so on).
Publishing the Package
Step 5: Publish Your Package to npm
Run the following command inside your project folder:
npm publish
If successful, you’ll see output similar to:
+ my-awesome-library@1.0.0
Your package is now available at:
📌 https://www.npmjs.com/package/my-awesome-library
Step 6: Making Changes and Updating the Package
If you want to release a new version, update the version
field in package.json
. npm follows Semantic Versioning (SemVer):
- Patch: Bug fixes (1.0.0 → 1.0.1)
- Minor: New features, backward-compatible (1.0.0 → 1.1.0)
- Major: Breaking changes (1.0.0 → 2.0.0)
Instead of manually updating package.json
, use:
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
Then, publish the new version:
npm publish
If you accidentally publish a package and need to remove it:
npm unpublish my-awesome-library --force
Note
You can only unpublish packages within 72 hours of publishing.
🎯 You’ve successfully published your own npm library! Now, other developers can install it using:
npm install my-awesome-library
By following Semantic Versioning, writing clear documentation, and maintaining your package, you contribute to the open-source ecosystem and make your code reusable.