Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Command line arguments (argv)

  • process
  • argv

Throught the process object we can access the command line of our program. The first value is node itself. The second value is our code, then come the values from that the user supplied.

console.log(process.argv.length);
console.log(process.argv);
console.log('')

console.log(process.argv[0]);
console.log(process.argv[1]);
console.log(process.argv[2]);
console.log(process.argv[3]);
console.log(process.argv[4]);
$ node argv.js "hello world" Foo
4
[
  '/opt/node-v12.14.1-linux-x64/bin/node',
  '/home/gabor/work/slides/nodejs/examples/basic/argv.js',
  'hello world',
  'Foo'
]

/opt/node-v12.14.1-linux-x64/bin/node
/home/gabor/work/slides/nodejs/examples/basic/argv.js
hello world
Foo
undefined