DEV Community

Cover image for How to publish a CLI tool on NPM
Mohammed Nadeem Shareef
Mohammed Nadeem Shareef

Posted on

5 3

How to publish a CLI tool on NPM

Hello Developers 🙌🙌🙌

Recently I wanted to create a CLI tool, but I only know JavaScript, so I researched and found out that we can create CLI applications using JavaScript. I published my CLI tool named track-task and will show you how to publish a package in NPM. You can download and use it.

npm i -g track-task
Enter fullscreen mode Exit fullscreen mode

How to publish NPM package?

Step 1:- Initialize npm by npm init -y
It will create a default package.json file, which will look something like

{
  "name": "track-task",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
Enter fullscreen mode Exit fullscreen mode

Points to note here

  • Name should be unique, otherwise it will throw error while publishing.
  • Start with version 1.0.0 and remember to update to version whenever you make changes to your code.
  • Add a bin as " bin "./index.js" from this point your code will run.
  • Add keywords and author name to stand out.

Step 2:- Code your application.
Code your whole application and test it out locally and make sure to add #! /usr/bin/env node, it makes your file executable.

Step 3:- Add more info to package.json.
Add repository, bugs and homepage urls

{
  ...
  "repository": {
    "type": "git",
    "url": "git+https://github.com/shareef99/task-tracker.git"
  },
  "bugs": {
    "url": "https://github.com/shareef99/task-tracker/issues"
  },
  "homepage": "https://github.com/shareef99/task-tracker/#readme",
}
Enter fullscreen mode Exit fullscreen mode

Step 4:- Publishing your CLI tool

  • Create npm account.
  • Login into npm account from terminal.
npm login
Enter fullscreen mode Exit fullscreen mode
  • Enter the username and password.
  • Make sure you are ready to publish.
  • Publish your package.
npm publish
Enter fullscreen mode Exit fullscreen mode

Congratulations ✨✨✨ You have published the npm package.

Step 5:- Updating package.
Don't forget to change the version of in package.json file after update your code. Once you are done with the coding, run npm publish.

Thanks and Happy Coding

Closing here 👋👋👋

This is Shareef.
My Portfolio
Twitter ShareefBhai99
Linkedin
My other Blogs

SurveyJS custom survey software

Build Your Own Forms without Manual Coding

SurveyJS UI libraries let you build a JSON-based form management system that integrates with any backend, giving you full control over your data with no user limits. Includes support for custom question types, skip logic, an integrated CSS editor, PDF export, real-time analytics, and more.

Learn more

Top comments (0)

Migrate from EVM to Rust

Migrate from EVM

Migrate your smart contracts from Solidity to Rust. Let us know how we can help.

Make the Move

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay