Ateur Games
Ateur Games

Reputation: 79

Take input from node call nodejs

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.

Upvotes: 0

Views: 74

Answers (2)

Yogesh.Kathayat
Yogesh.Kathayat

Reputation: 1004

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

Upvotes: 2

mllduran
mllduran

Reputation: 86

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/

Upvotes: 1

Related Questions