ReferenceError: prompt is undefined. How would I fix this in JavaScript?
Asked Answered
C

8

7

I am working on creating different meal menus in this restaurant I have created using JavaScript. I was on my first couple lines of added code when I came across this ReferenceError saying that prompt is undefined. If you need to see full context you can reference the original code here https://repl.it/@qwerty_________/Welcome-to-the-Drivethrough and the code I'm working on here https://repl.it/@qwerty_________/DullFreeMisrac. The official error is right here.

ReferenceError: prompt is not defined
at evalmachine.<anonymous>:2:12
at Script.runInContext (vm.js:74:29)
at Object.runInContext (vm.js:182:6)
at evaluate (/run_dir/repl.js:133:14)
at ReadStream.<anonymous> (/run_dir/repl.js:116:5)
at ReadStream.emit (events.js:180:13)
at addChunk (_stream_readable.js:274:12)
at readableAddChunk (_stream_readable.js:261:11)
at ReadStream.Readable.push (_stream_readable.js:218:10)
at fs.read (fs.js:2124:12)

This is on JavaScript. I have already tried changing it to window.prompt rather than just prompt and it said that window was undefined.

	var nam = prompt("Hi, Welcome to the Drivethrough!! What is your name?")
	var mprice = 4
	var bprice = 8
	var cprice = 6
	var fprice = 2
	var oprice = 3
	var fmprice = 5
	var gprice = 0
	var price = 0
	var fcmprice = 10
	var cmprice = 4
	var kkkprice = 9
	var dddprice = 10
	var mmmprice = 12
	var icprice = 6
	var resprice = 999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999
	var superprice = 1

I was expecting it to bring up the popup box asking me my name and then me select the breakfast menu to test out chocolate milk, but it instead brought up the error code telling me that the prompt was undefined.

Thank you so much for taking a look at my question. :)

Com answered 1/2, 2019 at 19:50 Comment(0)
H
7

It seems you run this code in a Node.js environment, in the site that provides this environment. window object is not defined in this environment. You can try to test you script in a browser console or from inside an HTML file.

Hoover answered 1/2, 2019 at 19:59 Comment(2)
What is the alternative that works in the terminal?Valor
@RadovanBabjak See nodejs.org/api/readline.htmlHoover
A
14

Open terminal and run:-

Step 1: npm init
Step 2: npm install prompt-sync
Step 3: Open the js file (name.js) in which you want to use prompt and import the prompt-sync as given below. (on line 1)

eg: const prompt = require("prompt-sync")();


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

let a = prompt("enter a number: ")
console.log(a)
Account answered 25/7, 2022 at 7:56 Comment(1)
Ok, running your code in a browser is a way to fix the issue. But this answer is more useful if the intent is to stick to the node.js environment.Averse
L
10

prompt() is a method of the Global window object found in browsers. It's not part of the JavaScript language. If you don't run your code in a browser or an environment that supports the Browser Object Model, window won't be there and if window isn't there, neither will prompt() be. Run your code in a browser.

Livialivid answered 1/2, 2019 at 19:57 Comment(1)
What is the alternative that works in terminal?Valor
H
7

It seems you run this code in a Node.js environment, in the site that provides this environment. window object is not defined in this environment. You can try to test you script in a browser console or from inside an HTML file.

Hoover answered 1/2, 2019 at 19:59 Comment(2)
What is the alternative that works in the terminal?Valor
@RadovanBabjak See nodejs.org/api/readline.htmlHoover
S
5

I get out of this problem this way:

  1. Go to Terminal then choose new terminal and run cmd npm install prompt-sync again run your code if it showing same error then follow 2nd step

  2. At the header section of your code type: const prompt = require ("prompt-sync")({sigint: true})

Example:

const prompt = require("prompt-sync")({sigint:true});
let work = prompt("It will definitely works for you :- ")
Spurt answered 10/9, 2022 at 6:23 Comment(1)
You should use code formatting in your answerTyrocidine
H
2

It happens because of you run this code in a Node.js environment(VS Code). Window object is not defined in this environment. You can try to test you script in any browser (chrome, firefox etc.) console, where you can easily access this code.

Hashish answered 14/8, 2022 at 20:59 Comment(0)
V
1

To address the issue of the ReferenceError: Prompt is not defined in VS Code or prompt not working, you can follow the steps below:

  1. Open your terminal or command prompt.

  2. To clear the screen, type the command clear and press Enter.

  3. Initialize a new npm project by running the command npm init -y in your terminal or command prompt. This will create a package.json file with default values.

  4. Install the prompt-sync package by running the command npm install prompt-sync in your terminal or command prompt. This package allows synchronous user input.

  5. At the top of your code file, add the following line to import the prompt-sync module and create a prompt object:

    const prompt = require('prompt-sync')();

  6. Save your code file.

  7. Run your code and check if the prompt functionality is working correctly.

I was able to solve the problem by referencing the YouTube video: How to Fix ReferenceError Prompt is Not Defined in VS Code - Prompt is Not Working in VSCode. The video by GameTrick

Visit answered 4/7, 2023 at 11:0 Comment(0)
D
0

I get out of this problem this way:

open cmd then run npm install prompt-sync after running it will show "added 3 packages in 2s"

now At the header section of your code type:

const prompt = require ("prompt-sync")({sigint: true})

Example:

const prompt = require("prompt-sync")({sigint:true});
let a=prompt("Hey whats ur age?");
Dongdonga answered 24/4, 2023 at 20:0 Comment(0)
S
0

First Open VS Code IDE

run this cmd $npm init -y

Then run $npm install prompt-sync and Add the below line in your code on top.

const prompt = require('prompt-sync')();

Hurry!!! It's ready to use node in the program.

Symposiarch answered 13/11, 2023 at 13:25 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Phelps

© 2022 - 2025 — McMap. All rights reserved.