Node.js Command Line Arguments

To access command line arguments in a Node.js script file, use the process.argv array. This array contains the values passed when the Node.js process is started from a terminal, command prompt, shell script, or npm script.

Command line arguments are useful when a script should accept input without editing the source code. For example, you may pass two numbers to a calculator script, a file name to a converter script, or an environment name such as dev or prod to a deployment script.

In this tutorial, we shall learn how to read Node.js command line arguments using process.argv, how to ignore the first two default values, how to pass arguments that contain spaces, and how to handle simple named options.

How process.argv stores Node.js command line arguments

The process.argv value is an array. Its first two items are added by Node.js automatically.

  • process.argv[0] is the path of the Node.js executable.
  • process.argv[1] is the path of the JavaScript file being executed.
  • process.argv[2] and the following items are the arguments passed by the user.

For most scripts, you will read values from index 2 onward. The common way to do that is process.argv.slice(2).

Example 1 – Read Arguments from Command Line

In this example, we will read command line arguments programmatically using process.argv, and print them to console.

command-line-args-example.js

</>
Copy
// process.argv is the array that contains command line arguments
// print all arguments using forEach
process.argv.forEach((val, index) => {
  console.log(`${index}: ${val}`);
});

Output

~$ node command-line-args-example.js argument_one argument_two 3 4 five
0: /usr/local/nodejs/bin/node
1: /home/tutorialkart/workspace/nodejs/command-line-args-example.js
2: argument_one
3: argument_two
4: 3
5: 4
6: five

By default argument 0 is the path to node program and argument 1 is the path to the Node Java Script file. The rest are the additional arguments provided to the Node.js. White space characters are considered as separators for the arguments.

Read only user supplied command line arguments in Node.js

If you do not need the Node.js executable path and script file path, create a new array using slice(2). This gives only the arguments passed after the script name.

read-user-args.js

</>
Copy
const args = process.argv.slice(2);

console.log(args);

Run the script with three arguments.

</>
Copy
node read-user-args.js apple banana cherry

Output

[ 'apple', 'banana', 'cherry' ]

This is the simplest pattern for most small Node.js command line scripts.

Pass numbers as command line arguments in Node.js

Values in process.argv are strings. If an argument represents a number, convert it before doing arithmetic.

sum.js

</>
Copy
const args = process.argv.slice(2);

const firstNumber = Number(args[0]);
const secondNumber = Number(args[1]);

if (Number.isNaN(firstNumber) || Number.isNaN(secondNumber)) {
  console.log('Please provide two valid numbers.');
  process.exit(1);
}

console.log(firstNumber + secondNumber);

Run the script as shown below.

</>
Copy
node sum.js 12 8

Output

20

If you use the values directly without conversion, JavaScript treats them as strings in many operations. For example, '12' + '8' gives '128', not 20.

Pass command line arguments with spaces in Node.js

Shells split command line input by spaces. To pass one argument that contains spaces, wrap the value in quotes.

message.js

</>
Copy
const args = process.argv.slice(2);

console.log(args);

Run the script with a quoted message.

</>
Copy
node message.js "Hello from Node.js" production

Output

[ 'Hello from Node.js', 'production' ]

Without quotes, the words Hello, from, and Node.js would be received as separate arguments.

Read named options from Node.js command line arguments

For simple scripts, you can read options such as --name and --count manually. The following example converts command line arguments into an object.

named-options.js

</>
Copy
const args = process.argv.slice(2);
const options = {};

for (let i = 0; i < args.length; i += 2) {
  const key = args[i];
  const value = args[i + 1];

  if (key && key.startsWith('--')) {
    options[key.slice(2)] = value;
  }
}

console.log(options);

Run the script with named options.

</>
Copy
node named-options.js --name John --role admin

Output

{ name: 'John', role: 'admin' }

This manual approach is fine for basic scripts. For larger command line tools with aliases, default values, boolean flags, and help messages, use a dedicated argument parser or Node.js utilities designed for command line applications.

Use Node.js command line arguments from npm scripts

When you run a script through npm run, add -- before the arguments that should be forwarded to your Node.js file.

package.json

</>
Copy
{
  "scripts": {
    "start-report": "node report.js"
  }
}

report.js

</>
Copy
const args = process.argv.slice(2);

console.log(args);

Run the npm script and forward arguments to report.js.

</>
Copy
npm run start-report -- sales 2026

Output

[ 'sales', '2026' ]

The separator -- tells npm that the following values should be passed to the script instead of being treated as npm options.

Node.js options versus script arguments

Node.js also has its own command line options. For example, --version prints the installed Node.js version, and --inspect starts the debugger. These options are different from the arguments you pass to your script.

</>
Copy
node --inspect app.js dev

In this command, --inspect is used by Node.js, while dev is passed to app.js as a script argument.

Common mistakes with Node.js command line arguments

  • Reading from index 0 by mistake: User arguments usually start from process.argv[2], not process.argv[0].
  • Forgetting that arguments are strings: Convert values with Number(), Boolean checks, or custom validation when needed.
  • Not quoting values with spaces: Use quotes when one argument contains multiple words.
  • Confusing Node.js options with script arguments: Node.js options come before the script file, while script arguments come after the script file.
  • Skipping validation: Check that required arguments are present before using them in file operations, calculations, or network calls.

Editorial QA checklist for this Node.js command line arguments tutorial

  • Confirms that process.argv[0] and process.argv[1] are default Node.js values.
  • Shows process.argv.slice(2) for reading only user supplied arguments.
  • Explains that command line arguments are received as strings.
  • Includes an example for quoted arguments that contain spaces.
  • Separates Node.js runtime options from script arguments.

Node.js command line arguments FAQs

How do I take command line arguments in Node.js?

Use the process.argv array. The first two values are added by Node.js, so user supplied arguments usually start at process.argv[2]. You can use process.argv.slice(2) to get only the arguments passed after the script file name.

What is an example of a command line argument in Node.js?

In the command node app.js production, the value production is a command line argument. Inside app.js, it can be read from process.argv[2].

Why does process.argv include two extra values?

Node.js includes the path of the Node.js executable at index 0 and the path of the executed script at index 1. The actual user arguments begin from index 2.

How do I pass an argument with spaces to a Node.js script?

Wrap the value in quotes. For example, use node app.js "monthly sales report" so that monthly sales report is received as one argument.

Are Node.js command line arguments always strings?

Yes. Values in process.argv are strings. Convert them to numbers, booleans, dates, or other types before using them in type-sensitive operations.

Node.js command line arguments summary

In this Node.js Tutorial, we have learnt how to provide and access command line arguments in a Node.js script file. Use process.argv when you need the full argument array, and use process.argv.slice(2) when you need only the values supplied by the user.