Mayank Kumar
Posted on November 23, 2024
This week, I successfully released my CLI tool, dev-mate-cli
, to the public npm registry. It was a rewarding experience that involved learning and refining my project. Here’s a detailed account of how I did it, along with lessons I learned and tips for anyone looking to release their own package.
Choosing a Release Tool and Registry
As my project was written in TypeScript and designed as a Node.js CLI tool, I chose npm
for hosting my package. It’s the most popular package registry for JavaScript developers, and I was already familiar with using it to install dependencies. Publishing to npm
made sense, as it would make my tool accessible to a broad audience.
You can create an account at npmjs.com. Once your account is ready, publishing a package involves using simple and well-documented commands.
Process for Releasing
-
Preparing the Project for Release
- I added a
bin
field in mypackage.json
file to make my CLI executable. This field maps the CLI command (dev-mate-cli
) to the script that should run when the command is invoked. - Ensured all needed dependencies, like
dotenv
, were correctly listed in the dependencies field ofpackage.json
rather thandevDependencies
. This is critical becausedevDependencies
are not installed when users download the package.
- I added a
-
Testing Locally with
npm link
- Before publishing, I ran
npm link
to simulate how users would install and use the package globally. This helped me test the CLI commands and catch any errors.
- Before publishing, I ran
-
Building the Project
- Since the project was written in TypeScript, I ensured my build script compiled the source code correctly. Running
npm run build
generated the necessary JavaScript files in adist
folder.
- Since the project was written in TypeScript, I ensured my build script compiled the source code correctly. Running
-
Publishing the Package
- After verifying everything worked locally, I logged into npm using
npm login
and published the package. - The
--access public
flag was required since I wanted my package to be publicly available.
npm publish --access public
- After verifying everything worked locally, I logged into npm using
User Testing
For the testing phase, I asked a fellow developer to try out dev-mate-cli
. The tool worked as intended, and she found it easy to use with the provided instructions. Her main suggestion was improving the README to make the setup process clearer. Specifically, she recommended placing critical setup steps (like environment variable configuration) at the top of the documentation, which I promptly implemented.
Lessons Learned
-
Managing Dependencies: One of the key issues I encountered was the
dotenv
module being listed as adevDependency
. Since npm doesn’t includedevDependencies
in the published package, this caused a runtime error. This was a valuable reminder to double-check the dependencies section before publishing. -
Streamlining Documentation: User testing with a developer revealed areas for improvement in my README. For instance, moving instructions for setting up environment variables (
API_KEY
andBASE_URL
) to the installation section made it easier for users to get started.
Overall, publishing dev-mate-cli
was a rewarding process, filled with valuable lessons. I hope this blog helps other developers navigate the npm publishing process more easily. If you’re building your own CLI tool, feel free to reach out with questions or suggestions!
Posted on November 23, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.