node.js, getting command line arguments

In node js, the command line arguments are stored in process.argv. The first argument is always “node” and the second argument is always the file name of the nodejs script, the third and the following are arguments you can pass into the nodejs script. For example, this piece of code will print all the command line arguments when you run it. /home/getCommandLineArgs.js
[code language=”javascript”]
for(var i=0; i<process.argv.length; i++)
console.log(process.argv[i]);
[/code]

For command: node /home/getCommandLineArgs.js
[code language=”text”]
node
/home/getCommandLineArgs.js
[/code]

For command: node /home/getCommandLineArgs.js abc
[code language=”text”]
node
/home/getCommandLineArgs.js
abc
[/code]

For command: node /home/getCommandLineArgs.js abc 123
[code language=”text”]
node
/home/getCommandLineArgs.js
abc
123
[/code]

Search within Codexpedia

Custom Search

Search the entire web

Custom Search