Max SchmittMS
7th June 2020

Deeply nested subcommands in Node CLIs with Commander.js

For a recent project I was leveraging the awesome Commander.js framework to build out a CLI with a ton of commands and subcommands.

Example: Fast-food CLI

Let's say you're building a CLI to order food from a fast food restaurant with the following interface:

Program overview:
$ mcd
Order some food:
$ mcd order
Order a burger menu:
$ mcd order burger
Order some dessert:
$ mcd order dessert
Pay a meal:
$ mcd pay

File structure

With Commander.js, you can easily implement above interface using the following file structure:


😍 Note how neatly self-documenting this file structure is.

Each of these files is a standalone executable and could be used on its own (don't forget to run chmod +x cli/*).


The most top-level program (mcd) will declare its subcommand order.


#!/usr/bin/env node
const { program } = require('commander')
// mcd-order
program.command('order', 'Order some food')
program.action(() => {

Important: program.command must be invoked with the second argument (description), as this will hint to Commander that the subcommand lives in its own executable.

The subcommand mcd-order will declare its own subcommands using the same syntax:


#!/usr/bin/env node
const { program } = require('commander')
// mcd-order-burger
.command('burger', 'Order a burger menu')
// mcd-order-dessert
.command('dessert', 'Order some dessert')
program.action(() => {

And finally, at the end of the chain, mcd-order-burger does not declare any more subcommands:


#!/usr/bin/env node
const { program } = require('commander')
program.action(() => {
console.log('Thank you for ordering some burgers!')

And that's how you can deeply nest subcommands for your Node.js CLI using Commander.js!

For a complete reproducible example, check out the repository on GitHub.

Image of my head

About the author

Hi, I’m Max! I'm a fullstack JavaScript developer living in Berlin.

When I’m not working on one of my personal projects, writing blog posts or making YouTube videos, I help my clients bring their ideas to life as a freelance web developer.

If you need help on a project, feel free to contact me.

To stay updated with new blog posts, follow me on Twitter or subscribe to my RSS feed.