Commander.js: Creating Powerful Console Applications on Node.js
In the world of modern web development and automation, console applications (CLI) play a key role. They allow you to automate routine tasks, manage projects, deploy code, and interact with APIs without using a graphical interface. If you are writing in JavaScript (Node.js), the best choice for creating elegant and functional CLI tools is the Commander.js framework.
Commander.js is a lightweight but incredibly powerful library that handles all the routine of parsing command line arguments, generating help, and error handling. With it, you can turn a simple script into a professional console application with support for subcommands, options, and automatic validation. In this article, we will take a detailed look at how to use Commander.js on the educational website pythonlib.ru to create your own CLI utilities.
Installation and Basic Usage
Before starting, make sure you have Node.js installed (version 12 and above). Installation of Commander.js is done via npm:
npm install commanderAfter installation, create a file cli.js and include the library. Let's look at a simple example — an application that outputs "Hello, World!" and accepts a user name.
#!/usr/bin/env nodeconst { Command } = require('commander');const program = new Command();
program .name('greet') .description('Outputs a greeting') .version('1.0.0');
program .command('hello') .description('Greets the user') .argument('<name>', 'User name') .action((name) => { console.log(`Hello, ${name}! Welcome to Commander.js.`); });
program.parse(process.argv);Now run the script:
node cli.js hello IvanYou will see: Hello, Ivan! Welcome to Commander.js.
Main Features of Commander.js
Commander.js provides a rich set of functions for creating professional CLI applications. Let's look at them in detail.
1. Commands and Subcommands
You can create an unlimited number of commands (e.g., init, build, deploy) and nest them within each other. This is especially useful for complex tools like package managers or project builders.
program .command('init') .description('Initializes a new project') .option('-t, --template <type>', 'Project template (basic, advanced)') .action((options) => { console.log(`Creating project with template: ${options.template || 'basic'}`); });2. Options (Flags)
Options allow the user to pass additional parameters. Commander.js supports short (-v) and long (--verbose) flags, as well as mandatory and optional values.
program .option('-d, --debug', 'Enable debug mode') .option('-p, --port <number>', 'Server port', '3000') // default value .parse(process.argv);
if (program.opts().debug) { console.log('Debug mode enabled');}3. Command Arguments
Arguments are positional parameters that the user passes after the command. They can be mandatory (<name>) or optional ([name]).
program .command('copy <source> [destination]') .description('Copies a file from source to destination') .action((source, destination) => { console.log(`Copying from ${source} to ${destination || 'current folder'}`); });4. Automatic Help and Versioning
Commander.js automatically generates help when using the --help flag or
Also in library
protobuf for C++: complete guide with examples
GraphQL in PHP: Complete Guide to the webonyx/graphql-php Library
Working with CSV in PHP: Reading, Writing, and Data Processing
Elasticsearch client for C# (.NET): NEST and Elastic.Clients.Elasticsearch
Working with Kafka in Rust: rdkafka and Franz Libraries