How to publish a CLI tool on NPM

How to publish a CLI tool on NPM

ยท

2 min read

Hi everyone. ๐Ÿ™Œ๐Ÿ™Œ๐Ÿ™Œ

Recently I found and noted this 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 daily-routine and will show you how to publish a package in NPM. You can download and use it.

npm i -g daily-routine

How to publish the NPM package?

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

{
  "name": "daily-routine",
  "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

  1. The name should be unique, otherwise, it will throw an error while publishing.
  2. Start with version 1.0.0 and remember to update to the version whenever you make changes to your code.
  3. Add a bin as " bin "./index.js" from this point your code will run.
  4. Add keywords and author names 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 an npm account.
  • Login into the npm account from a terminal.
    npm login
    
  • Enter the username and password.
  • Make sure you are ready to publish.
  • Publish your package.
npm publish

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

Did you find this article valuable?

Support ramu k by becoming a sponsor. Any amount is appreciated!

ย