title: Line number of SyntaxError in Node.js
body: <p>I have some code that uses <code>vm</code> module and <code>runInNewContext</code> function and executes dynamically generated JavaScript code. Basically a safer option of <code>eval</code>.</p>
  
  <p>The code (variable <code>code</code>) can possibly contain syntax errors, so I would like to catch them and print some useful information.</p>
  
  <pre><code>try {
      vm.runInNewContext(code, sandbox, filename);
  }
  catch (e) {
      if (e instanceof SyntaxError) { // always false
          console.log(e.toString()); // "SyntaxError: Unexpected token ||" for example
          console.log(e.line); // how to get the line number?
      }
  }
  </code></pre>
  
  <p>I'd like to print the number of the line with the syntax error, but I have two problems:</p>
  
  <ul>
  <li>I don't know how to recognize whether the exception is <code>SyntaxError</code> or something else. <code>instaceof</code> doesn't work (update - I can use <code>e.name === "SyntaxError"</code>).</li>
  <li>Even if I was able to recognize it, how could I get the line number? Is it possible?</li>
  </ul>
  
  <p>Thanks in advance.</p>
  
  <p>Update: I can get some information from <code>e.stack</code> - however, the topmost call in the stack trace is <code>runInNewContext</code> (with its line number), but I still can't find the line number inside <code>code</code>, which caused the exception (<code>SyntaxError</code>).</p>