0

Just wondering, how do you take input directly from the node call? I know you can do it in Python. Here is an example of what I want to achive:

Code:

function main(x,y) {
 return x * y;
}

Command Call:

node index.js 2 2

Or something.

2 Answers 2

2

you can use process.argv

let inputArr=process.argv.slice(2);
console.log(inputArr) // [2,2]

we are skipping the first two element of the array process.argv because the first two elements will be always - node and the path to your script

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

1 Comment

Both are good, but I marked this answer because it added an easy example.
1

You can use process.argv this will return an array containing the arguments you passed in CLI

Ref: https://nodejs.org/en/knowledge/command-line/how-to-parse-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.