Dev Linux Docker Misc

9 JavaScript Libraries To Create The Coolest CLIs

4 min read 15 October 2022 On Dev

Lets look at libraries that help us to create the coolest command line applications.

JavaScript is a great tool to build incredible CLIs that can be easily installed and provide great user experience. I've personally used all the 9 libraries we'll see below 👇

1. Itivrutaha

Output of using itivrutaha logger module

A super fancy way of console.log() that supports emoji, outputs in colors and can even write to a file. It is specially designed for usage in CLIs with support for advanced features like multiple log instances with different contexts.

Support for colors and emoji are handled properly and with flexible themeing options and most importantly, it is created by me 😉

2. Conf

import Conf from 'conf'

const config = new Conf()

config.set('unicorn', '🦄')
config.get('unicorn')
// => '🦄'

// dot-notation to access nested properties
config.set('foo.bar', true)
console.log(config.get('foo'))
// => { bar: true }
Example usage of conf npm module

Different operating systems save config files differently, With conf you don't need to worry about where to store configuration keys for your CLI program.

It is highly configurable and supports advanced features like migration, encryption and storing in different data formats with a sleek API.

3. Commander

Automatic help generated by commander npm module

It is the most popular and feature rich command line argument parser with extensive docs, easy-to-use API and automatic help generation. I have tried to use several libraries but I always come back to it.

Advanced features like variadic options, custom option parsing, sub commands are all available. Personally I find it's help messages a bit bland, so I created rupa which outputs help messages in a colorful way 👇

Output of commander made colorful by rupa npm module

4. Listr2

Example output of using listr2 npm module

Lets you create really cool task lists that can be easily understood by the user while conveying the progress of the work. You can create sub-tasks, conditionally run tasks or skip them or new add tasks on the fly.

For non-interactive environments, Listr2 handles verbose and silent logging perfectly 👌

5. Execa

import { execa } from 'execa'

const { stdout } = await execa('echo', ['unicorns'])

// piping the child process stdout to the parent
execa('echo', ['unicorns']).stdout.pipe(process.stdout)
Example usage of execa npm module

It is a wrapper around the built-in child_process module to make it easy to run shell commands inside our CLIs.

With improved Windows support, bigger buffer for stdio, excellent docs and a promise based interface it is the goto library to run system commands inside CLIs.

6. CLI Table 3

Example output of using cli-table3 npm module

Render out tabular data in the terminal to output human-readable data in a concise way with this module.

This module is a balance between stablity and features as it is very popular, once you choose to use it, you'll forgot about it 😄

7. Inquirer

Example prompts created using inquirer npm module

Add interactive prompts to your CLIs ✨

It is extendable and stable with lots of big projects depending on it. Several types of prompts are pre-built with good docs. Using inquirer along with listr2 and itivrutaha can create a superb user experience.

In addition to that, for taking complicated user input through a text editor, checkout read-file-input from me 😊

8. ASCII Art

Sample ascii art created using ascii-art npm module

Lets you create a cool artwork that can be shown when the CLI starts. It is more extensive compared to lot of popular alternatives with advanced features like support for custom figlet fonts, color profile support and much more.

9. Pkg

Lets you bundle your amazing CLI into a single binary file that doesn't require Node.js to be installed on the user's computer. It also protects from tampering the code as JavaScript can be easily modified.

Several other alternatives exist like caxa, nexe but this one is well maintained by Vercel.

Conclusion

Providing good user experience is equally important for CLIs as much as offering speed and functionality. With these 9 npm modules, JavaScript CLIs can be polished to give the best user experience along great functionality.

Thank you 😊