Skip to content

The Command Line and the process Object: Building a CLI

Programming Node.js Difficulty 1/5

Beginning of the lesson

The Command Line and the process Object: Building a CLI

Teach your Node programs to listen to the terminal and talk back

Big Idea: Every time you type node app.js, Node hands your program a special helper called process. It carries the words you typed after the file name, the settings of your computer, and the pipes for reading input and printing output. Once you know how to use it, you can build a real command-line tool - a program that takes instructions from the terminal.
By the end you will be able to:
  • read the words typed after node app.js using process.argv,
  • read computer settings with environment variables (process.env),
  • print output with process.stdout and read typed input from process.stdin,
  • end your program with an exit code using process.exit,
  • put it all together into a small CLI (command-line interface) tool.

1. What is a command-line tool?

A command-line tool (also called a CLI, short for command-line interface) is a program you run by typing its name in the terminal, often with a few extra words that tell it what to do. You have used many already! When you type:

node app.js
npm install express
git commit -m "hello"

...each of those is a command-line tool. The first word is the program (node, npm, git), and the words after it are arguments - the extra instructions.

Analogy. Think of a CLI like ordering at a sandwich counter. You do not just say "sandwich" - you say "sandwich, no onions, extra cheese". The word "sandwich" is the program, and "no onions" and "extra cheese" are the arguments that change what you get. Your Node program is the sandwich maker: it reads your order and builds exactly what you asked for.

In this topic you will build your own sandwich counter - a program the user can boss around from the terminal.

Continue this lesson on YourAIZone

The full lesson has the complete explanation, worked examples, common mistakes, tests with instant checking and an AI tutor. Start right away as a guest, or create an account to keep your progress.

In the full lesson

  • The complete explanation
  • Worked examples
  • Common mistakes and how to avoid them
  • Test questions: 15
  • AI tutor that answers questions about the topic
  • Progress tracking for students, parents and teachers