Hey there! If you’ve ever tried building a JavaScript program outside a browser, you might have run into the dreaded ReferenceError: prompt is not defined
. I sure did and it stopped me in my tracks. Let me walk you through how I fixed this error, improved my code, and turned a simple script into a practical tool.
Why prompt
Broke My Code
I was working on a simple project in VS Code: a weight converter and an annual profit calculator. The code worked fine in my head, but when I ran it, I got this error:
ReferenceError: prompt is not defined
Turns out, prompt
is a browser-specific function. When you’re coding in Node.js (which VS Code uses to run JavaScript outside browsers), prompt
doesn’t exist. My mistake? Assuming browser tools work everywhere. Lesson learned!
Bringing prompt
to Node.js
To fix this, I needed a Node.js-friendly way to get user input. Here’s what I did:
- Installed
prompt-sync
:
This npm module mimics the browser’sprompt
function. I installed it via the terminal:
npm install prompt-sync
- Replaced
prompt
withprompt-sync
:
I added this line at the top of my code:
const prompt = require('prompt-sync')();
What I Added
Instead of just fixing the error, I decided to turn my script into a user-friendly app. Here’s how:
Input Validation
My original code crashed if the user typed non-numeric values. I added checks to handle invalid inputs:
const weightInLbs = parseFloat(prompt("Enter weight in pounds (lbs): ")); if (isNaN(weightInLbs)) { console.log("Invalid input. Please enter a number."); return; // Exit the function early }
No more crashes when someone types “ten” instead of 10
!
Interactive Menu
Why run two separate programs when I could combine them? I built a menu system:
function mainMenu() { while (true) { console.log("\n=== Main Menu ==="); console.log("1. Weight Converter"); console.log("2. Profit Calculator"); console.log("3. Exit"); const choice = prompt("Choose an option (1-3): "); switch (choice) { case '1': weightConverter(); break; case '2': profitCalculator(); break; case '3': return; // Exit the program default: console.log("Invalid choice. Try again."); } } }
Now users can pick tools without restarting the program.
Cleaner Output Formatting
I used template literals and toFixed()
to format results neatly:
console.log(`Converted weight: ${weightInKg.toFixed(3)} kg`); // Output: "Converted weight: 45.359 kg" console.log(`Projected profit: $${profit.toFixed(2)}`); // Output: "Projected profit: $2345.67"
How to Run the Program
- Save the code as
app.js
. - Open VS Code’s terminal.
- Run:
node app.js
- Follow the menu prompts!
What I’d Add Next
- More Unit Conversions: Let users convert kg to lbs, Celsius to Fahrenheit, etc.
- Save Results to a File: Use
fs
module to log calculations. - Real-Time Currency Converter: Fetch exchange rates via an API.
Final Thoughts
Fixing the prompt error was just the beginning of improving my script. By adding input validation, an interactive menu, and cleaner output formatting, I transformed a basic program into a practical tool. It’s a great reminder that coding is not just about fixing errors, but about continuously improving and refining our work to make it more user-friendly and functional.