title: Javascript module conditionally execute a function by the execution context environment
body: <p>I write a module with some functions, and execute the functions in the module. </p>
  
  <p>My object is: </p>
  
  <p>If I run the module alone, those functions can be executed. But if it is imported by another function as a module, then those functions should not be executed.</p>
  
  <p>Like the code as below for demonstration:</p>
  
  <p>In the module, for example, moduleFunction.js:</p>
  
  <pre><code>function moduleFunction(){
    // some code run here
  }
  moduleFunction();  // run the function in standalone script, but not when import the module.
  export default moduleFunction;
  </code></pre>
  
  <p>In another module, for example runModule.js:</p>
  
  <pre><code>import moduleFunction from "moduleFunction";
  moduleFunction();
  </code></pre>
  
  <p>I already know I can achieve the task in Python by <code>if __name__ == "__main__":</code> .
  Is there a way to get it done in JavaScript?</p>