CLI command line tool

How to create a package that can be used as cli (and package)?

Folder Structure

"files": [
"dist",
"bin"
],
"main": "./dist/index.js",
"bin": {
"my-cli": "./bin/my-cli"
},
  • So the flow is:

    • We tell package the bin file is in the bin folder

    • The bin file invokes the actual script in dist folder

    • We develop the script in lib folder and babel the source ('prepublish') to dist folder

    • To test locally on dev machine

      npm link
      # then start using the cli version
    • To publish internally, do

      npm version minor # update version
      npm pack # this will create the abc-1.0.0.tgz file
      # to install and use
      npm install -g abc-1.0.0.tgz

Reference:

Useful Libs

A list of cli tools: https://github.com/sindresorhus/awesome-nodejs#command-line-apps