Considering the Scope
I've been analyzing the user's need for a Node.js CLI boilerplate. My focus is now on the core technologies: Node.js and commander.js. The goal is a functional starting point, so I'm considering the minimum features and structure needed to meet requirements, such as handling commands and options. I'm aiming for simplicity and flexibility in this design.
Outlining the Framework
I'm now zeroing in on the essential structure. I'm thinking of a straightforward setup with package.json, an entry point (bin/index.js), and a src/ directory for commands. This will ensure users have a clear starting point. I'm also planning for clear setup instructions. I also decided to use ES Modules, as the best practice.
Building the CLI Structure
I'm solidifying the CLI's structure, focusing on the bin/index.js file now. It'll have the shebang line, import commander and the package.json, and set up the program with version and description. Argument parsing is key, so that is included. Next, I'm fleshing out commands, starting with a simple "hello" command to demonstrate arguments and options. I'm separating command logic into a dedicated commands/ directory for cleaner organization. I have also decided to use the fs module to read the package.json file for versioning.
Defining Core Dependencies
I've decided on the core components for the CLI. I'm focusing on fs/readFile for package management, and I'm refining the file structure. I'm building out bin/index.js to initialize the CLI with commander. I'm starting the commands using a "greet" function. I'm also adding logger.js as an optional utility. I am making sure to provide clear file structure descriptions for the user.
Finalizing the Boilerplate
I've added the "greet" command logic and introduced chalk for console styling, enhancing the user experience. The file structure is now complete, including src/commands/greet.js and a refined bin/index.js. I've also drafted a concise guide covering setup, usage, and local linking with npm link, ensuring users can easily understand and run the generated CLI.
Tackling User Education
I'm now focusing on user experience, specifically addressing potential confusion for beginners building CLIs. I'm prioritizing clear explanations and helpful comments within the code. I'm focusing on making setup as easy as possible. My next step will be to thoroughly review the code.
Here is a modern, production-ready boilerplate for a Node.js CLI tool using Commander.js.
I have structured this using ES Modules (modern JavaScript) and included separation of concerns by splitting the entry point from the logic.
1. Project Structure
Create a folder for your project and set up the following files:
my-cli-tool/
├── bin/
│ └── index.js # Entry point / Executable
├── src/
│ └── commands/ # Command logic
│ └── greet.js
├── package.json
└── .gitignore
2. File Contents
package.json
Two key things here: type: "module" for ESM support and the bin object to map your command name to the file.
{
"name": "my-cli-tool",
"version": "1.0.0",
"description": "A boilerplate CLI using Commander.js",
"type": "module",
"main": "bin/index.js",
"bin": {
"my-cli": "./bin/index.js"
},
"dependencies": {
"chalk": "^5.3.0",
"commander": "^11.1.0"
}
}
bin/index.js
This is the main entry point. It defines the program, version, and registers commands.
Note: The #!/usr/bin/env node shebang at the top is required.
#!/usr/bin/env node
import { Command } from 'commander';
import { greet } from '../src/commands/greet.js';
const program = new Command();
program
.name('my-cli')
.description('CLI to some JavaScript string utilities')
.version('1.0.0');
program
.command('greet')
.description('Greet a user')
.argument('<name>', 'user to greet')
.option('-u, --upper', 'convert to uppercase')
.option('-c, --color <type>', 'output color', 'green')
.action((name, options) => {
greet(name, options);
});
program.parse();
src/commands/greet.js
This file holds the logic. Moving logic out of bin/index.js keeps your code clean as the app grows. I've added chalk for colored output (standard for CLIs).
import chalk from 'chalk';
export function greet(name, options) {
let output = `Hello, ${name}!`;
if (options.upper) {
output = output.toUpperCase();
}
// Apply color based on options
if (options.color && chalk[options.color]) {
console.log(chalk[options.color](output));
} else {
console.log(chalk.green(output));
}
}
.gitignore
Standard node ignore file.
node_modules/
.DS_Store
3. Installation & Setup
Follow these steps to get it running on your machine.
1. Initialize and Install Dependencies
Open your terminal in the project folder:
# Install the dependencies defined in package.json
npm install
2. Make the file executable (Linux/Mac only)
If you are on a Mac or Linux, you might need to grant permission:
chmod +x bin/index.js
3. Link the command locally
This feature allows you to test the command system-wide while developing.
npm link
4. Usage
Now that you have linked the package, you can run the command name defined in your bin section of package.json (my-cli).
Basic command:
my-cli greet "John Doe"
With options (Uppercase):
my-cli greet "John Doe" --upper
With options (Custom Color):
my-cli greet "John Doe" --color blue
View Help:
Commander auto-generates help documentation.
my-cli --help
5. Recommended Extras
To make this a full "Pro" level CLI, consider adding these libraries later:
- Inquirer.js: For interactive prompts (Yes/No, Inputs, Select lists).
- Ora: For loading spinners.
- Figlet: For ASCII art banners.