How to use argv in Node.js

Learn how process.argv in Node.js captures command line arguments and how to use it effectively in your scripts with practical examples.
Published 2023-11-04 5 min read
How to use argv in Node.js

In Node.js, process.argv is an array containing all the command line arguments passed when the Node.js process was launched.

ARGV is a concept known from the majority of modern programming languages. It stands for “Argument Vector” and was introduced by BCPL, which heavily influenced C. C then added its own spin on the idea, simplifying it a bit. In C, ARGV represents an array of strings (a list of characters) where each string corresponds to one of the arguments that were passed to the program from the command line.

Now that the history lesson is over, you probably figured out that Node.js has adopted this concept. We can go through some examples and learn how argv works in Node.

Example of argv in Node

Here’s a simple example. Let’s say you have a Node.js script named script.js that contains this code:

console.log(process.argv);

Now you run it from the command line with some arguments like so:

node script.js firstArg secondArg

This will output something like:

[
  "/path/to/node", // The first element: path to the Node.js executable
  "/path/to/script.js", // The second element: path to the script file
  "firstArg", // Additional arguments start here
  "secondArg",
];

Node.js also provides process.argv0, which is a property that specifically holds the first argument from the process.argv array, which is the path to the Node.js executable. This is useful when you need to refer to the Node.js instance that is running the script.

For convenience, Node.js allows you to import argv directly using destructuring, which can make your code cleaner and more readable. Instead of always typing process.argv, you can write:

import { argv } from "node:process";

Now you can refer to argv directly in your code, which will reference the same array as process.argv:

console.log(argv);

This will output the same array as process.argv would, allowing you to work with command line arguments in a more straightforward way.

When you’re working with Node.js, understanding how to handle command line arguments can be quite useful. Let’s dive into a practical example to see how process.argv works in action.

Node.js ARGV quirks

Let’s look at the output of our example script (with additional comments) to understand how it works in Node.

[
  "/Users/usr/.nvm/versions/node/v18.18.0/bin/node", // Node executable
  "/Users/usr/script.js", // The script file
  "123", // The argument passed to the script
];

Here, each file path is shown as an absolute location, which is the full path from the root of the filesystem to the file. This is different from C or many other languages.

The first element of the array is the path to the Node.js executable. This is consistent across all Node.js scripts and is a handy reference point if you ever need to know where Node.js is installed on your system.

Even if you make your script directly executable by adding a shebang line at the top of your script.js file like this:

#!/usr/bin/env node

console.log(process.argv);

And then run your script directly from the command line:

./script.js 123

The output will still show the process.argv array with Node.js as the first argument, indicating that Node.js was used to execute the script.

This demonstrates that process.argv behaves consistently, whether you’re invoking Node.js explicitly or using a shebang to make the script executable.

Key things to remember about argv in Node

Here are some key points to remember about process.argv in Node.js:

  • The first argument is always the path to the Node.js executable.
  • The second argument is the path to the JavaScript file being executed.
  • All the values in process.argv are strings, even if they look like numbers. So, remember about type conversions if needed.
  • process.argv is a regular JavaScript array, so you have access to all the standard array methods.

Since process.argv is just a regular array, you can manipulate it as you would with any array in JavaScript. For instance, you can use array slicing to ignore the first two elements and work only with the additional arguments:

const args = process.argv.slice(2);

This args array will now contain only the arguments passed to the script, excluding the Node.js executable and the script file paths. This is a common practice when you’re interested in the arguments specifically meant for your script’s logic.

You’ve probably noticed that the argv provided by Node.js is pretty basic; that’s why CLI tools often depend on 3rd party libraries for handling command line arguments.

#javascript #nodejs