Node.js by Example: Command-Line Arguments

Command-line arguments are a common way to parameterize execution of programs. For example, node hello.js uses hello.js as an argument to the node program.

process.argv provides access to raw command-line arguments. Note that the first value in this array is the path to the program.

const argsWithProg = process.argv;
const [_, ...argsWithoutProg] = process.argv;

You can get individual args with normal indexing.

const arg = process.argv[3];
console.log(argsWithProg);
console.log(argsWithoutProg);
console.log(arg);
$ node command-line-arguments.js a b c d
[
  '.nvm/versions/node/v22.16.0/bin/node',
  'command-line-arguments.js',
  'a',
  'b',
  'c',
  'd'
]
[
  'command-line-arguments.js',
  'a',
  'b',
  'c',
  'd'
]
b

Node.js does not have more advanced command-line processing, such as flags.

Next example: .