6

I'm trying to run a simple javascript program from the command line and am getting an unexpected error. prompt seems to be a simple javascript command; not sure why it's not defined.

my test.js file:

console.log("I will now ask you for your name.");
var name = prompt("Enter your name");
console.log("Hello ".concat(name, ". How are you"));

I'm running it like this:

C:\directory\node test.js
I will now ask you for your name.

C:\directory\test.js:2
var name = prompt("Enter your name");

Reference Error: prompt is not defined
3
  • 2
    You probably want to use readline. window.prompt() is a function that web browsers implement, but it is not a native function in node.js. Commented Jun 18, 2014 at 17:45
  • So lets say I simply want to run this code and see the results, what is the best way to do this? My school told us to install node and use that but it seems like that is the wrong way to go about it judging by your responses Commented Jun 18, 2014 at 17:56
  • 1
    Node.JS is JavaScript... Commented Oct 1, 2015 at 19:12

5 Answers 5

3

you might wanna check this out as well but first run npm install prompt -- save

var prompt = require("prompt");
prompt.start();
console.log("I will now ask you for your name.");
prompt.get(["name"], function(err, res){
console.log("Hello ".concat(res.name, ". How are you"));
});
Sign up to request clarification or add additional context in comments.

1 Comment

Node does not implement window.prompt() and using a lib to emulate this is a bad idea. Instead, the OP should use readline.
3

“PROMPT” is a JavaScript Function that is available when you are using JS in browsers (Google Chrome, Safari, Microsoft Edge, etc.), and what this allows us to do is take users' inputs.

Now, when running your code outside the browser, Node.js doesn’t have access to this function, so instead, you are going to need to install a Node.js module called “prompt - sync”.

STEPS :

1.- Go to your windows terminal, copy and paste the following command.

npm install prompt-sync   (make sure you have NPM and Node JS installed on your system)

2.- Now, you have to load this module in your JS program, your code should look something like this.

const ps = require("prompt-sync");

const prompt = ps();

console.log("I will now ask you for your name.");

var name = prompt("Enter your name: ");

console.log("Hello ".concat(name, ". How are you?"));

and this will solve the issue👍.

Comments

2

I think you might be interested in this code :

console.log("I will now ask you for your name.");
process.stdin.setEncoding('utf8');
var name;
process.stdin.on('readable', function() {
    name = process.stdin.read();
    if (name !== null) {
        console.log("Hello ".concat(name, ". How are you"));
        process.exit();
    }
});

this is the node.js way of doing what you need to do. Hope this helps!

Comments

1

Node.js does not support prompt out of the box like web browsers do. Node.js does have it's own version of prompt called readline

const readline = require("readline");
let rl = readline.createInterface(process.stdin, process.stdout);
rl.question("What is your age? ", (age) => {
  console.log("Your age is: " + age);
});

Read this article for more information https://www.geeksforgeeks.org/node-js-readline-module/

Comments

-1

In Node.js, there’s no built-in prompt() like in browsers. So if you want to take input from the user in the terminal, you need to use packages like prompt-sync. npm install prompt-sync const prompt = require('prompt-sync')(); simple example const name = prompt('What is your name? '); console.log(Hello, ${name}!);

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.