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]

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

Jørgen is right, but mind that "alex" is process.argv[2] in the example.
Thanks, I included it in the answer :)
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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.