Prompt the User in the Terminal
Ask a question on stdin with node:readline, wait for the answer as a promise, then close the interface.
import readline from "node:readline";
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
const answer = await new Promise((resolve) => {
rl.question("Your name? ", resolve);
});
rl.close();
console.log(`Hello ${answer}`);
Caveats
ES module with top-level await: run as .mjs, or set type module in package.json. Needs a TTY. Empty input is an empty string, not null.