Command Line

Node.js Command Line Interfaces

As a backend developer, your terminal (or command line) is your best friend. Node.js applications are launched, tested, and managed entirely through command-line interfaces (CLI).

In this tutorial, we will explore how to interact with Node.js via the command line, pass arguments to your scripts, and manage execution flow.


1. Running Scripts

The most common task you will perform is executing a JavaScript file. You simply use the node command followed by the name of the file.

# Assuming you are in the directory where index.js is located
$ node index.js

If your file doesn't have an infinite loop or an active server listening for requests, Node will execute the code from top to bottom and automatically return you to your terminal prompt when finished.


2. The process Object

Node.js provides a global object called process. This object provides incredibly useful information about, and control over, the current Node.js process running in your terminal.

Because process is a global object, you do not need to use require() to import it.

Terminating a Script Early

Sometimes, an error occurs, and you want to shut down your script immediately. You can use process.exit().

Exiting a Node script

console.log("Program starts...");

const password = "wrong_password";

if (password !== "secret123") { console.log("Access Denied! Shutting down."); // Exit code 1 indicates an error/failure process.exit(1); }

// This line will NEVER run if the password is wrong console.log("Welcome to the secret area!");

Pro Tip: Exit code 0 means the program succeeded, while any non-zero number (like 1) signals that an error occurred.


3. Passing Command Line Arguments

You can pass custom data into your Node script directly from the terminal. This is useful for writing CLI tools or scripts that need dynamic input.

For example, imagine you run your file like this:

$ node greet.js Akash Admin

How do you access the words "Akash" and "Admin" inside greet.js?

You use the process.argv array. (argv stands for argument values).

Reading Arguments in greet.js

// Print the entire array of arguments
console.log(process.argv);

Output:

[
  '/usr/local/bin/node',  // Index 0: Absolute path to the node executable
  '/Users/akash/greet.js',// Index 1: Absolute path to the file being executed
  'Akash',                // Index 2: First custom argument
  'Admin'                 // Index 3: Second custom argument
]

Because the first two elements in the array are always system paths, your custom arguments always start at Index 2. Let's write a script that actively uses them:

Using the Arguments

// Use real CLI args when available, otherwise fall back to a demo value.
const args = process.argv.length > 2 ? process.argv : ['node', 'greet.js', 'John'];

// Make sure an argument was provided if (args.length < 3) { console.log("Please provide a name! Example: node greet.js John"); process.exit(1); }

const userName = args[2]; console.log(Welcome to Node.js, ${userName}!);


4. Environment Variables

Another crucial part of the command line experience in Node is process.env. It holds all environment variables currently set on the computer (like API Keys, database passwords, or port numbers).

Reading an Environment Variable

// If no PORT environment variable is set, default to 3000
const port = process.env.PORT || 3000;

console.log(Server will run on port: ${port});


Exercise

?

At which array index does the first custom command-line argument appear in process.argv?