DEV Community

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

Posted on

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

Top comments (0)