title: user input with node.js
body: <p>I have the following Node.js code that behaves strangely:</p>
  
  <pre><code>#!/usr/bin/env node
  
  "use strict";
  
  var readline = require('readline');
  
  var rl = readline.createInterface({
      input: process.stdin,
      output: process.stdout
  });
  
  function input(prompt) {
      rl.question(prompt, function (x) {
          rl.close();
  
          console.log("debug: " + x);
          return x;
      });
  }
  
  function main() {
      var n = input("Number: ");
  //    console.log("value: " + n);    // problematic line
  }
  
  main();
  </code></pre>
  
  <p>I want to mimic Python's <code>raw_input</code>, i.e. reading a line from the user. After showing the prompt, the program should be blocked until the user presses Enter. </p>
  
  <p>If the "problematic line" is in comments, it works, the program is waiting for input. However, if this line is not in comments, then the program doesn't wait for input and <code>n</code> becomes <code>undefined</code>. Why? How to write a function that returns the user input?</p>