How do you take user inputs in Javascript(NodeJs)?

Published: (December 10, 2025 at 01:48 AM EST)
1 min read
Source: Dev.to

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.

Back to Blog

Related posts

Read more »

Code Block Click Copy

I'm a back‑end‑leaning full‑stack developer, and after a long day fixing JavaScript code I decided to add “copy to clipboard” buttons to the code blocks on my s...