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

17,045

Solution 1

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.

Solution 2

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.

Share:
17,045

Related videos on Youtube

anz621
Author by

anz621

Working hard on learning JavaScript just for fun.

Updated on June 04, 2022

Comments

  • anz621
    anz621 almost 2 years

    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. :)

  • Radovan Babjak
    Radovan Babjak about 3 years
    What is the alternative that works in terminal?
  • Radovan Babjak
    Radovan Babjak about 3 years
    What is the alternative that works in the terminal?
  • vsemozhebuty
    vsemozhebuty about 3 years
    @RadovanBabjak See nodejs.org/api/readline.html