Create a Vanilla NodeJS CLI
Hi there 👋🏾. I'm a software engineer that enjoys building stuff and talking about them. I also tinker a bit with hardware and robotics using Arduino and ROS.
Let's say you want to get your feet wet with creating CLIs. What's a better way to do that than creating a very basic hello-world CLI? In this tutorial, we will create a CLI that greets us Hello ${name} using Node.js only.
What is a CLI
A Command Line Interface (CLI) is a computer program that collects input as text and displays output as text. CLIs are run in shells like Bash in Linux, CMD and Powershell in Windows. If you have ever run a Javascript file using NodeJS or used git commands, then you have used a CLI. Both Node and Git are CLIs which are run in shells.
Extra Credits: What about REPLs and Interactive shells?
Great question. A REPL is an acronym for Read Eval Print Loop. As the name implies, a REPL
- reads a set of instructions in form of text
- evaluates those instruction to fetch or modify data
- prints the result as output to the user and
- repeats the process.
An interactive shell, on the other hand, runs like a REPL but may prompt the user for input. Python's interactive shell is a good example. If you run the script below, you will be prompted to input the first and second number.
REPLs and Interactive Shells are special cases of CLIs. They are usually invoked from a parent shell like bash although they can run as independent programs. Developers will encounter shells at several points in their careers. In fact, some developers use it daily. It never hurts to learn the tools you use deeply.
Wrap Up
CLIs are great automation tools that will simplify your development workflow. Learning how to build one may come in handy now or later. To continue your learning, look into
- OClif: A Node.js framework for building CLIs, used by Heroku.
- Yargs: An npm package for collecting CLI input
Feel free to share what you built with me on Twitter. Please subscribe to my newsletter (top of the page) so you don't miss these sort of tutorials. Thanks for reading. Adios ✌🏾🧡.
