Nadeem Shareef
Shareef

Follow

Shareef

Follow
How to publish a CLI tool on NPM

How to publish a CLI tool on NPM

Nadeem Shareef's photo
Nadeem Shareef
·May 30, 2022·

2 min read

Play this article

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

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"
}

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",
}

Step 4:- Publishing your CLI tool

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

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

Did you find this article valuable?

Support Nadeem Shareef by becoming a sponsor. Any amount is appreciated!

See recent sponsors | Learn more about Hashnode Sponsors
 
Share this