There's something satisfying about writing code that solves a problem in your own project. But there's an even better feeling when that same code becomes useful to someone else.
At some point, I started thinking: Why should I keep writing the same logic across different projects when I can package it once and reuse it anywhere?
That's where npm packages come in.
Publishing a package isn't just about uploading code to a registry. It's about making your work reusable, accessible, and easier for other developers to integrate into their projects. Whether you're building a utility library, a reusable Angular component, a JavaScript helper, or a complete SDK, publishing it to npm makes sharing and maintaining your work much easier.
In this article, I'll walk through the basics of creating, preparing, and publishing an npm package—from a local project to something anyone can install.
![A terminal window showing the successful output of an npm publish command, with the package name, version, and tarball contents listed.]
A successful npm publish—the final step after configuration, testing, and authentication.
Why create an npm package?
We've all written helper functions, reusable components, validators, configurations, or utilities that end up being copied from one project to another.
It works, until maintaining those copies becomes a problem.
A small improvement in one project doesn't reach the others. A bug gets fixed in one place but remains in another. Eventually, you have multiple versions of the same logic.
Packaging that code solves this problem. Instead of copying files between projects, you can maintain a single package and install it wherever you need it.
npm install my-package
Simple. Reusable. Maintainable.
Every time you run npm install lodash, you're downloading a package published by someone else. But npm also gives you the ability to become the publisher. That's the beauty of open-source and package ecosystems—write once, share everywhere.
Getting started
Let's start with a simple package.
mkdir my-package
cd my-package
npm init -y
This generates a package.json file, which contains the metadata npm needs to understand your package—its name, version, entry point, dependencies, and other configuration.
{
"name": "my-package",
"version": "1.0.0",
"description": "A collection of reusable utilities",
"main": "index.js",
"license": "MIT"
}
Think of package.json as the identity of your package. It tells npm and other developers what your package is and how to work with it.
Your package name must be unique in the npm registry unless you publish under a scope. For example: my-package, @aakash/utils, or @my-company/shared.
I prefer scoped packages when publishing under a personal or organization namespace, because it helps keep related packages organized and prevents name collisions.
![A flowchart showing how to choose package visibility: scoped public for everyone, private for limited users.]
Choosing between scoped public, unscoped public, and private packages.
Write something reusable
For this example, let's create a small utility function.
function greet(name) {
return `Hello, ${name}!`;
}
module.exports = { greet };
Nothing complicated—and that's the point.
You can test it locally:
const { greet } = require('./index');
console.log(greet('Developer'));
// Hello, Developer!
A package doesn't have to be a massive library with hundreds of features. It can be a small collection of utilities that solves a specific problem really well. Sometimes a small, well-designed utility is more useful than a large library trying to do everything.
The important part is that the code is useful outside the project where it was originally written.
Make it ready for others
Before publishing, I like to think about one thing:
If someone installs this package without knowing anything about my project, can they use it easily?
That means having a clean structure, a clear README, proper metadata, and no unnecessary files.
A basic package structure could look like this:
my-package/
├── index.js
├── package.json
├── README.md
├── LICENSE
└── .gitignore
Add a README
Your README is often the first thing developers see when they visit your package page. It should clearly explain what the package does, why someone should use it, how to install it, how to use it, API documentation, examples, and license.
Don't underestimate documentation. A package with good documentation is much easier to adopt than a package with clever code but no explanation.
Control what you ship
If you want to explicitly exclude files from your published package, you can use a .npmignore file. However, npm already ignores certain files and respects package.json's files field, so you can control package contents without always needing .npmignore.
For example:
{
"files": [
"index.js",
"README.md"
]
}
Then check what files will be included before publishing:
npm pack --dry-run
This is a small step, but it can save you from accidentally shipping files you don't need—missing entry points, broken configs, or .env files that should never leave your machine.
![Terminal output of npm pack --dry-run showing exactly which files would be published.]
Preview the tarball before publishing—no surprises.
Publishing to npm
Once the package is ready, it's time to make it public.
First, create an account on npmjs.com if you haven't already. Then log in from your terminal:
npm login
Follow the authentication steps in your browser or terminal, depending on the npm CLI flow.
Verify that you're authenticated:
npm whoami
If it returns your npm username, you're ready to publish.
npm publish
For public scoped packages, you may need:
npm publish --access public
That's it. Your package is now available through the npm registry, and other developers can install it.
npm install my-package
And use it:
const { greet } = require('my-package');
console.log(greet('World'));
The first time you see your own package available in the registry, it feels a little different. Something that previously existed only in your local code is now accessible to anyone.
![A horizontal flow diagram showing the four-step publish process: write code, npm pack --dry-run, bump version, npm publish, then npm view to verify.]
From code to registry—preview, version, publish, verify.
Versioning matters
Publishing is easy. Maintaining a package is where things get interesting.
Once people start using your package, changes become more important. A small update might fix a bug, introduce a new feature, or completely change how something works.
This is where semantic versioning comes in.
MAJOR . MINOR . PATCH
-
Patch — Bug fixes and small improvements (
1.0.0 → 1.0.1) -
Minor — New features without breaking existing functionality (
1.0.0 → 1.1.0) -
Major — Breaking changes (
1.0.0 → 2.0.0)
npm makes updating versions straightforward:
npm version patch
npm version minor
npm version major
After updating your version, publish again:
npm publish
Remember: npm doesn't allow overwriting an already published version. Each published version must be unique. Following a consistent versioning strategy helps users understand what changed and whether upgrading is safe.
Making your package developer-friendly
Publishing a package is only the beginning. A good package should be easy to install, understand, maintain, and integrate.
Some things worth considering:
- TypeScript support: Include type definitions so developers get autocomplete and type safety.
-
ESM and CommonJS: Consider supporting modern
importand traditionalrequire()consumers when relevant. - Testing: Add unit tests to make sure future updates don't break existing functionality.
- CI/CD: Automate testing and publishing workflows using GitHub Actions.
- License: Make it clear how people are allowed to use your code.
- Security: Never publish secrets, API keys, tokens, or environment files.
For a TypeScript library, your package.json might eventually include fields like:
{
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
}
}
These fields help Node.js, bundlers, and TypeScript consumers resolve your package correctly. For TypeScript authors, bundling declaration files with the package is standard practice—set the types field to point to your .d.ts file, and ensure that any @types dependencies your declarations rely on are listed in dependencies rather than devDependencies.
One underused feature is staged publishing. Instead of publishing directly, npm stage publish submits the package to a staging area. A maintainer can then review and approve it with 2FA before it becomes publicly available. This is particularly useful for CI workflows: the CI job can stage the package without 2FA, but a human must approve the final release.
What I learned
Publishing a package made me think differently about the code I write.
Code that works inside my application isn't automatically ready to be consumed by others. Reusability requires a little more thought—how the API is designed, how errors are handled, how the package is documented, and how future changes will affect existing users.
It's easy to focus only on writing the code. But when you publish something, you're also taking responsibility for the experience of the developer who installs it.
A package can be published in a few minutes, but maintaining a good package is an ongoing responsibility. As people start using it, you might receive issues, discover bugs, improve performance, add features, or rethink the API. A good maintainer keeps dependencies updated, responds to issues, maintains backward compatibility where possible, writes clear changelogs, and deprecates features responsibly.
The goal isn't just to put code on npm. The goal is to create something that developers can depend on.
And that, to me, is what makes creating packages interesting.
A small step toward building in public
Not every project needs to become an npm package. But when you find yourself solving the same problem repeatedly, it might be worth extracting it and sharing it.
You don't need to build something groundbreaking. Start small. Solve a real problem. Make it easy to use. Document it. Publish it.
Publishing an npm package is a small step technically, but a meaningful step as a developer. It pushes you to think beyond your own application—to consider API design, documentation, versioning, compatibility, and how other people will interact with your code.
Someone else might be facing the exact same problem you did.
And maybe your few lines of code will save them an hour of work.
That's one of the things I love about software development: we don't always have to build everything from scratch. Sometimes, the most valuable thing we can do is share what we've already built.
Write it once. Make it reusable. Share it with the world.
llouts carry the actual information.
Author: Aakash Rajbanshi




Top comments (0)