You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求助:AWS Cloud9中JS prompt报错及while循环语法正确性疑问

Hey there! Let's tackle your issues step by step

First: The "ReferenceError: prompt is not defined" error

The prompt() function is a browser-specific API — it’s built into web browsers to get input from frontend users. But AWS Cloud9’s JS compiler runs in a Node.js (server-side JavaScript) environment, which doesn’t include this function by default. That’s exactly why you’re seeing that error.

To get user input in Node.js (like Cloud9), you can use the built-in readline module instead. Here’s a simple replacement for prompt():

const readline = require('readline');

// Set up the input/output interface
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

// Ask a question and handle the user's response
rl.question('Enter your input here: ', (userInput) => {
  console.log(`You entered: ${userInput}`);
  // Close the interface when finished
  rl.close();
});

If you need to pair input with a while loop (for repeated prompts), wrap the question in a promise and use async/await for cleaner flow:

const readline = require('readline').createInterface({
  input: process.stdin,
  output: process.stdout
});

// Helper function to turn readline questions into promises
function ask(query) {
  return new Promise(resolve => rl.question(query, resolve));
}

async function runRepeatedPrompts() {
  let userInput;
  // Loop until the user types "exit"
  while (userInput !== 'exit') {
    userInput = await ask('Type something (or "exit" to stop): ');
    console.log(`Got it: ${userInput}`);
  }
  rl.close();
}

runRepeatedPrompts();

Second: Checking your while loop syntax

A valid while loop in JavaScript follows this core structure:

// Correct basic structure
while (condition) {
  // Code to run repeatedly as long as the condition is true
}

Common syntax mistakes to watch for:

  • Forgetting parentheses around the condition: while x < 10 { ... } (invalid — always wrap the condition in ())
  • Omitting curly braces for single-line code blocks (allowed but risky, as adding lines later can break the loop logic):
    // Works but not recommended
    while (count < 5) console.log(count++);
    
  • Accidentally creating an infinite loop (if your condition never becomes false and you don’t include a break):
    // Infinite loop unless you add an exit condition inside
    while (true) {
      if (someTriggerCondition) break;
    }
    

If your loop matches the correct structure above, the syntax is probably solid. If you’re still unsure, sharing your actual loop code would help pinpoint any issues.

And you’re totally right — Stack Overflow is perfect for these specific, code-focused questions. Including your code snippet, exact error message, and environment details (like AWS Cloud9/Node.js) will make it way easier for others to help you quickly.

内容的提问来源于stack exchange,提问作者Ranma Saotome

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 08:34:14