How to create your command-line program (CLI) with NodeJS and Commander.js | by Duc N. | JavaScript
Create your own command line
This post will show you how to create a command-line npm module (CLI) using Commander.js module.
Commander.js is a very popular module that lets you create your own CLI program.
First, start your new project — let’s say my project name is “json-now”
Now, create your package.json file:
Then, install dependencies:
The “bin” section specifies your command line name. As you see, go ahead and create a “bin” directory with “index.js” file there:
Let’s create the very first option called “-v” or “ — version” which shows version number. Create a directory named “lib” and a new file “ver.js” there:
So far, it looks straight forward. You created a commander “program” which handles option like “-v” by running “ver.js”
Open Terminal and try it out:
Now, it’s time to publish your command line for the world to use!
The above code is located here for your reference: GitHub - ngduc/json-now: $ json-now - Launch an API Server to serve data from a JSON, JS file or faker data with HTTPS support. Based on json-server.
Last updated