Publishing your first NPM package can seem daunting, but it's actually straightforward once you know the steps. In this guide, I'll walk you through the entire process, including how to solve common issues like registry configuration problems.
Prerequisites
- Node.js and npm installed
- A project ready to publish
- An NPM account (create one at npmjs.com)
Step 1: Check Package Name Availability
Before you start, make sure your desired package name isn't already taken:
npm search your-package-name
If the name is taken, you have several options:
- Use a scoped package:
@yourusername/package-name
- Choose a different name
- Add descriptive prefixes/suffixes
For example, if "testpilot" is taken, you could use "ai-testpilot" or "@yourusername/testpilot".
Step 2: Configure Your package.json
Make sure your package.json
has all the required fields:
{
"name": "your-package-name",
"version": "1.0.0",
"description": "What your package does",
"main": "index.js",
"repository": {
"type": "git",
"url": "git+https://github.com/yourusername/your-repo.git"
},
"keywords": ["keyword1", "keyword2"],
"author": "Your Name",
"license": "MIT",
"bugs": {
"url": "https://github.com/yourusername/your-repo/issues"
},
"homepage": "https://github.com/yourusername/your-repo#readme"
}
For CLI tools, add a bin
field:
{
"bin": {
"your-command": "./bin/cli.js"
}
}
Step 3: Fix Registry Configuration (Important!)
This is where many people get stuck. If you're using a mirror like Taobao or npmmirror, you need to switch back to the official NPM registry for publishing.
Check your current registry:
npm config get registry
If it shows anything other than https://registry.npmjs.org/
, fix it:
npm config set registry https://registry.npmjs.org/
Common problematic registries:
-
https://registry.npmmirror.com/
(Alibaba mirror) -
https://registry.npm.taobao.org/
(Taobao mirror) - Company internal registries
Step 4: Login to NPM
npm login
You'll be prompted for:
- Username
- Password
- Email address
Verify you're logged in:
npm whoami
Step 5: Prepare for Publishing
Create .npmignore (optional):
node_modules/
.git/
.DS_Store
*.log
test/
.env
If you're using TypeScript, build first:
npm run build
Test your package locally:
npm pack
npm install -g your-package-name-1.0.0.tgz
# Test your package
npm uninstall -g your-package-name
rm your-package-name-1.0.0.tgz
Step 6: Publish!
npm publish
For scoped packages, make them public:
npm publish --access public
Step 7: Verify Publication
Check on NPM website:
Visit https://npmjs.com/package/your-package-name
Search for your package:
npm search your-package-name
Test installation:
npm install your-package-name
Common Issues and Solutions
"Package name already exists"
- Use a scoped package:
@yourusername/package-name
- Choose a different name
- Check if the existing package is abandoned (contact NPM support)
"Public registration is not allowed"
- You're trying to publish to a private registry (like CNPM)
- Fix your registry configuration (Step 3)
"You do not have permission to publish"
- Make sure you're logged in:
npm whoami
- Check if you're trying to publish to the correct registry
- For scoped packages, use
--access public
"Avatar not updating"
- NPM uses Gravatar for avatars
- Link your NPM email to a Gravatar account
- Clear browser cache and wait for synchronization
Pro Tips
- Version Management: Use semantic versioning (major.minor.patch)
- Add a README: Include installation and usage instructions
- Keywords: Add relevant keywords for better discoverability
- License: Always specify a license (MIT is popular)
- Automated Publishing: Consider using GitHub Actions for CI/CD
Updating Your Package
When you need to update:
- Make your changes
- Update the version in
package.json
- Run
npm publish
again
# Quick version bump
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
npm publish
Conclusion
Publishing to NPM is straightforward once you understand the process. The most common stumbling block is registry configuration - always make sure you're pointing to the official NPM registry when publishing.
Your package is now available for the entire Node.js community to use. Congratulations on your first NPM publication! 🎉
Have you published your first NPM package? Share your experience in the comments below!
Top comments (0)