Skip to main content

Build a Basic CLI with Nodejs and Commander

Commander.js is a very popular module that lets you create your own CLI program.#

Create a app.js file and add the below boilerplate code.

// import commander
const program = require("commander");
// set the version number ( You can run node app --version )
program.version("1.0.0").description("Client Management System");
// Create a add command
.command("add <firstname> <lastname> <phone> <email>")
.description("Add a customer")
.action((firstname, lastname, phone, email) => {
console.log({ firstname, lastname, phone, email });
// Create a find command
.command("find <name>")
.description("Find a customer")
.action((name) => {
console.log(`Customer found ${name}`);
// Create a List command
.description("List all customers")
.action(() => {
console.log("List of customers");

Run node app --version

commander help

Run the add command

node commands.js add John Doe 0868907335
firstname: 'John',
lastname: 'Doe',
phone: '0868907335',
email: ''

Addition Resources#

Commander npm package
Inquirer npm package
Build CLI Video

Last updated on