As developers, we spend a surprising amount of time solving the same problems over and over again.
Keyboard shortcuts. Form validation. Date formatting. API wrappers. Utility functions.
Even across different projects, and sometimes different companies, we end up rewriting very similar code. Not because libraries don’t exist, but because the solution is often tightly coupled to a specific framework, project structure, or coding style.
This is where reusability becomes more than a convenience, it becomes a productivity multiplier.
When you extract a piece of logic into a reusable package, you:
reduce duplication,
improve consistency across projects,
and turn a one-off solution into something that can evolve independently.
Publishing to npm takes this one step further. Instead of reusing code only within your own projects, you make it available to anyone who faces the same problem. You also force yourself to think more clearly about:
API design,
documentation,
and long-term maintainability.
In this article, I’ll walk through the complete process of creating a small JavaScript package and publishing it to the npm registry — from structuring the project to testing it locally and finally making it publicly available.
The goal isn’t to build a complex framework, but to show how even a simple, focused utility can be turned into a proper package that others can use.
I have recently published my first package called Keyroute and will use it as an example.
Creating and publishing to npm
If you do not have angular or node, please do needful to install and have them
Create a workspace using:
ng new keyroute-workspace --create-application=falseGo in the workspace:
cd keyroute-workspaceIn the workspace, generate a library as follows:
ng generate library keyrouteIn projects/keyroute/src/lib/keyroute.ts
Write your logic.
It is where the actual re usable component sitsGo to the public-api.ts file. (projects/keyroute/src/public-api.ts)
Add the following line: export * from './lib/keyroute';
If your package is different, then add the name of your library instead of keyroute at the end.
In the package.json of package.json in the project folder, add the following:
"version": "<<version of your package based on your version system>>",
"description": "<<a description of what your package does>>",
"keywords": [
"keyboard",
"shortcuts",
"hotkeys",
"accessibility",
"angular",
"react",
"vue"
],
"license": "MIT"
}Run: ng build keyroute
This will create a ‘dist’ folder. This ‘dist’ folder is the one which is going to be pushed later to the registry
NOTE: Everytime you make a change, you need to run the build command so as to create the ‘dist’ folder, hence the code that will be sent to the registry.


Join the conversation! Your thoughts help the community grow.