title: How to check whether a script is running under Node.js?
body: <p>I have a script I am requiring from a Node.js script, which I want to keep JavaScript engine independent.</p>
  
  <p>For example, I want to do <code>exports.x = y;</code> only if it’s running under Node.js. How can I perform this test?</p>
  
  <hr>
  
  <p>When posting this question, I didn’t know the Node.js modules feature is based on <a href="http://www.commonjs.org/" rel="noreferrer">CommonJS</a>.</p>
  
  <p>For the specific example I gave, a more accurate question would’ve been:</p>
  
  <p>How can a script tell whether it has been required as a CommonJS module?</p>