How do you take user inputs in Javascript(NodeJs)?
Source: Dev.to
Using prompt‑sync
Node.js does not have a built‑in synchronous method for reading input from the terminal, so an external library such as prompt‑sync is commonly used.
Installation
npm install prompt-sync
Run the command in the terminal (e.g., VS Code integrated terminal) from the directory of your project.
Basic usage
// Import the library and create a prompt function
const prompt = require('prompt-sync')();
// Ask the user for input
const userInput = prompt('Enter your name: ');
// Output the entered value
console.log(userInput);
When the program runs, it will display the prompt text (Enter your name:).
If the user types David and presses Enter, the program prints:
David
This method provides a simple, synchronous way to read user input from the Node.js terminal.
Using readline (brief note)
The built‑in readline module can also be used to read input, but it works asynchronously and can be a bit more confusing for beginners. For now, prompt‑sync offers an easier entry point. You can explore readline later when you’re comfortable with the basics.