Pushlished on 09 Feb 2023
This code will run in browser/client side javascript.
const name = prompt('Enter your name');
console.log(name);
This code will run on server side javascript.
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
readline.question('Who are you?', name => {
console.log(`Hey there ${name}!`);
readline.close();
});
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);
}
});
};