(Want to jump straight to the result? Check out my package: cipher-utils.)
Before we move ahead to make a npm package,
First Things First, let's understand What Exactly Is NPM?
Because it helps to understand the ecosystem we're working with.
NPM stands for Node Package Manager, and it plays two massive roles in the world of JavaScript:
The Package Registry: It's more like a library (world's largest) of JavaScript. Developers from all over the globe publish their reusable pieces of code here, from tiny utility functions to massive frameworks. When you npm install react or npm install express, you're pulling code from this registry. This makes sharing and reusing code incredibly efficient for all the JS developers.
The Package Manager (CLI Tool): This is the command-line utility you use every day. It's the librarian of your projects, helping you:
- To Grab those packages from the registry and add them to your project. using command npm install
- Remove them when you're done npm uninstall
- Manage scripts defined in your package.json.
Understanding these two aspects makes the whole publishing process much clearer.
Now, let's get into the "how"! This is exactly what I did, from start to finish. Follow along!
Step 1: Get Your Code Package-Ready (Pure Functions)
My cipher algorithms (ROT13, Base64, XOR) were initially written as standalone scripts. They would either ask for input or read it from the command line, and then print the result. This is fine for quick testing, but for a reusable NPM package, functions need to be pure.
What "pure" means here:
- They take all their necessary inputs as function arguments.
- They return their result, rather than printing it to the console or interacting with the user.
- They don't have side effects (like modifying global variables).
My earlier code:
I applied the hermetic function logic to all the three algorithms
Note: Don't forget to export the function.
I am using type: module. This will be exported using ES Module syntax.
Step 2: Time to initialize the package
Before diving straight into package initialization, I set up proper version control by creating a Git repository and cloning it locally. This ensures clean tracking of my package's development from day one.
- Create a Repository on Github
- Clone the repo in your terminal
git clone git@github.com:armasahar/Cipher-Utils.git cd Cipher-Utils code . //this opens your cloned directory in VS code
Now once you are in VS code, let's get started with NPM initialization. Every NPM package needs a package.json file.
npm init
You'll be prompted with your package details in the VS code's terminal. Here’s what I entered:
Once you enter all the details you will be shown the formatted details, and ask you "is this okay?" you just have to press enter.
and it will create a package.json file in your root folder with all the details you have entered, you can edit them anytime.
Step 3: Structure Your Code with ES Modules
Modern JavaScript uses ES Modules (import/export). To use this in a Node.js package, you need a crucial setting.
Add "type": "module" in your package.json: This tells Node.js to interpret your .js files as ES Modules.
Organize your source files:
I created a src/ directory to hold my individual cipher files (rot13.js, base64.js, xor.js).
Create index.js (The Package's Entry Point): This file sits at your project's root and imports all your functions from src/, then re-exports them. This is what users will interact with. Remember to include the .js file extension in your local import paths for ES Modules!
This is my folder structure:
Add Documentation
README.md: This is your package's user manual.
It should explain:
- What the package is and what it does.
- How to install it (npm install cipher-utils).
- Detailed usage examples for each function (e.g., rot13('...'), base64.encode('...')).
- An API reference for all exported functions.
- Information on contributing and licensing.
LICENSE file: A plain text file containing the full text of your chosen license (like MIT). This tells people how they can use, modify, and distribute your code.
Step 5: Publish Your Package to NPM!
The moment of truth!
- login to npm.
npm login
(Once you login through terminal or through browser, get back to your codebase)
- Commit your code to Git: (Make sure all your hard work, code changes, package.json updates, new README.md – is committed. ALWAYS)
git add . && git commit -m "message" git push -u push origin
- Publish it now.
npm publish
If you face version error: (because I faced it)
If it's published well and good, but you might face an error here with the package version, so if you fall in this error just write
npm version patch
This will increase your package version to one level up.
and publish again.
npm publish
The feeling of seeing my own cipher-utils package live on npmjs.com was genuinely thrilling.
If you're on the fence about publishing your own project, I can't recommend it enough. It pushes you to polish your code, understand fundamental tools, and join a massive, supportive community.
What's next for cipher-utils? I'm already thinking about building a command-line interface (CLI) for quick terminal usage, and maybe adding more quirky ciphers.
For now, I hope my journey helps light the path for yours. Feel free to explore the package, give it a whirl, and even contribute!
NPM Package: cipher-utils
GitHub Repository: armasahar/Cipher-Utils
Thanks for reading!
Top comments (2)
It's a solid start, nicely done. You also might wanna set up something to handle changelogs, sync npm versions with git tags etc. It's a grind but ideally you only have to do it once per package 🙂
🫡 Sure, will do it!