3
node helloworld.js alex

I want it to console.log() alex. How do I pass "alex" as an argument into the code?

In python ,it is sys.argv[1]

xverges
  • 4,608
  • 1
  • 39
  • 60
TIMEX
  • 259,804
  • 351
  • 777
  • 1,080

2 Answers2

4

You can access command line arguments using process.argv in node.js.

The array also includes the node command and the application file, so the first custom command line argument will have an index = 2.

process.argv[2] === 'alex'; // true

http://nodejs.org/api/process.html#process_process_argv

Jørgen
  • 8,820
  • 9
  • 47
  • 67
0

If your requirements are more complex and you can take advantage of a command line argument parser, there are several choices. Two that seem popular are

More options available at How do I pass command line arguments?

Community
  • 1
  • 1
xverges
  • 4,608
  • 1
  • 39
  • 60