title: Using the dynamic import() function on Node.js
body: <p>I'm trying to implement basic functionality of a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import" rel="noreferrer">dynamic import</a> inside a Node v10 environment.</p>
  
  <p><strong>main.js</strong></p>
  
  <pre><code>async function main() {
    try {
      const moduleA = await import('./moduleA');
      console.log('Inside main function...');
      moduleA();
      console.log(moduleA);
    }
    catch(err) {
      console.log(err);
    }
  }
  
  main();
  </code></pre>
  
  <p><strong>moduleA.js</strong></p>
  
  <pre><code>console.log('Inside global scope module A...');
  
  function moduleA() {
    console.log('Running module A function...');
  }
  
  export default moduleA;
  </code></pre>
  
  <p>This is the result I'm getting when I run <code>npx babel-node main.js</code></p>
  
  <pre><code>PS C:\myProject\test-module-imports&gt; npx babel-node src/main.js
  Inside global scope module A...
  Inside main function...
  TypeError: moduleA is not a function
      at main (C:\myProject\test-module-imports\src/main.js:9:5)
      at process._tickCallback (internal/process/next_tick.js:68:7)
      at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
      at Object.&lt;anonymous&gt; (C:\myProject\test-module-imports\node_modules\@babel\node\lib\_babel-node.js:174:21)
      at Module._compile (internal/modules/cjs/loader.js:778:30)
      at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
      at Module.load (internal/modules/cjs/loader.js:653:32)
      at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
      at Function.Module._load (internal/modules/cjs/loader.js:585:3)
      at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
  </code></pre>
  
  <p><strong>.babelrc</strong></p>
  
  <pre><code>{
    "presets": [
      [
        "@babel/preset-env", {
          "targets": {
            "node": "10"
          }
        }
      ]
    ]
  }
  </code></pre>
  
  <p>I also tried this (the following code). And the result is the same:</p>
  
  <p><strong>main.js</strong></p>
  
  <pre><code>let moduleA = null;
  import('./moduleA').then((importedModule) =&gt; {
    moduleA = importedModule;
    main();
  });
  
  async function main() {
    try {
      // const moduleA = await import('./moduleA');
      console.log('Inside main function...');
      moduleA();
      console.log(moduleA);
    }
    catch(err) {
      console.log(err);
    }
  }
  
  // main();
  </code></pre>
  
  <p><strong>QUESTION</strong></p>
  
  <p>What am I doing wrong?</p>
  
  <hr>
  
  <p>This is working fine (regular import):
  Executing also with <code>npx babel-node src/main.js</code></p>
  
  <pre><code>import moduleA from './moduleA';
  
  async function main() {
    try {
      console.log('Inside main function...');
      moduleA();
      console.log(moduleA);
    }
    catch(err) {
      console.log(err);
    }
  }
  
  main();
  </code></pre>
  
  <p><strong>package.json</strong> (extra info)</p>
  
  <pre><code>{
    "name": "test-module-imports",
    "version": "1.0.0",
    "description": "",
    "main": "main.js",
    "scripts": {
      "test": "echo \"Error: no test specified\" &amp;&amp; exit 1"
    },
    "keywords": [],
    "author": "",
    "license": "ISC",
    "devDependencies": {
      "@babel/cli": "^7.7.0",
      "@babel/core": "^7.7.2",
      "@babel/node": "^7.7.0",
      "@babel/preset-env": "^7.7.1"
    }
  }
  </code></pre>