From the following code I can read and console.log the line
const readline = require('readline');
const { stdin: input, stdout: output } = require('process');
const rl = readline.createInterface({ input, output });
rl.question('What is your name? ', (answer) => {
console.log(`Hi ${answer}!`);
rl.close();
});
But I am trying to read user input like this
Input: node app.js John
Ouput: Hi John!
How can I achieve this in node?
To achieve that you can use the following:
process.argv
process.argv is an array containing all arguments that were used when invoking you programm.
Image you called your programm like that: node index.js "John", process.argv would look like this: ["node", "path/to/index.js", "John"].