Naman Arora

Getting User Input in JavaScript and Node.js ?

Pushlished on 09 Feb 2023

1) In JavaScript

This code will run in browser/client side javascript.

const name = prompt('Enter your name');
console.log(name);

2) In Nodejs

This code will run on server side javascript.

a) Synchronous Input using readline

const readline = require('readline').createInterface({
    input: process.stdin,
    output: process.stdout
});

readline.question('Who are you?', name => {
    console.log(`Hey there ${name}!`);
    readline.close();
});

b) Asynchronous input using Inquirer package

Install inquirer with

$ yarn add inquirer

or

$ npm i inquirer

Here is the sample code.

import inquirer from 'inquirer';
// or
const inquirer = require('inquirer');

const main = async () => {
    await inquirer
        .prompt([
            {
                type: 'input',
                name: 'name',
                message: 'Who are you ?'
            }
        ])
        .then(answers => {
            console.log(answers.name);
        })
        .catch(error => {
            if (error.isTtyError) {
                console.log(
                    "Prompt couldn't be rendered in the current environment"
                );
            } else {
                console.error(error);
            }
        });
};