Publishing your own NPM Package is too simple!!
Karthik Mudunuri / July 03, 2021
3 min read • ––– views
Introduction
Isn't it cool if you can run a command like npx <your_name>
in someone’s terminal, and they instantly get access to your portfolio, your works, or social handles — right from the CLI?
Or maybe you've built a dope React component. Wouldn't it be even cooler if other devs could use it just by installing it from npm?
That’s the power of open-source and npm — and yes, it’s ridiculously simple to get started!
Before diving into the how-to, try these in your terminal:
npx karthikmudunuri
ornpx get-response
— just to see what "cool" looks like.
🚀 Create your NPM account
Go to npmjs.com and sign up for a free account. Don’t skip 2FA (Two-Factor Authentication) — it keeps your packages safe. Use an app like TOTP Authenticator for better security.
🧠 Pick a Package Name
This must be unique! Visit the NPM Registry and search your desired name to make sure it's available.
If it’s already taken, get creative — maybe add a prefix or use a scoped package (@yourname/package
).
⚒️ Start Building the Package
✅ Install Node.js and NPM
If Node isn’t installed yet, head to nodejs.org and install the latest LTS version. You’ll get npm
bundled with it.
✅ Initialize Git
Create a project folder:
mkdir my-package && cd my-package
git init
✅ Initialize NPM
npm init
Answer the prompts:
- name: lowercase, hyphenated name.
- version: start with
0.1.0
- description: what your package does
- entry point: usually
index.js
- keywords: optional
- license: use default
ISC
Add this manually to package.json
:
"type": "module"
✍️ Write Your Code
Create index.js
:
#!/usr/bin/env node
console.log(
"Follow karthikmudunuri on GitHub: https://github.com/karthikmudunuri"
);
That first line allows execution from CLI using
npx
.
🧪 Add Executable Command
In package.json
, add:
"bin": {
"test": "./index.js"
}
Your full package.json
should look like:
{
"name": "test",
"version": "1.0.0",
"description": "just to test how to publish npm package",
"main": "./index.js",
"type": "module",
"bin": {
"test": "./index.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/karthikmudunuri/test.git"
},
"author": "Karthik Mudunuri",
"license": "ISC",
"bugs": {
"url": "https://github.com/karthikmudunuri/test/issues"
},
"homepage": "https://github.com/karthikmudunuri/test#readme"
}
🔐 Make it Executable
Linux/macOS:
chmod +x index.js
Windows (via Git Bash):
git update-index --chmod=+x index.js
🧪 Test Locally with npm link
Run:
npm link
Test globally outside the project:
npx test
📦 Publish to NPM
Login:
npm login
Publish:
npm publish
Now your package is live! 🎉
Visit npmjs.com (replace test
with your package name).
🔄 Update Your Package
- Bump version in
package.json
:
"version": "1.1.0"
- Re-publish:
npm publish
Make sure to follow Semantic Versioning.
✅ Conclusion
Now you’ve published an NPM package that can be installed and run from anywhere in the world. Whether it’s a React component or a personal CLI — you’ve contributed to open source.
If you found this helpful or want to improve it, ping me on GitHub
@karthikmudunuri
Want to hire me as a freelancer? Let's discuss.
Drop your message and let's discuss about your project.
Chat on WhatsAppDrop in your email ID and I will get back to you.