简体   繁体   中英

Using the dynamic import() function on Node.js

I'm trying to implement basic functionality of a dynamic import inside a Node v10 environment.

main.js

async function main() {
  try {
    const moduleA = await import('./moduleA');
    console.log('Inside main function...');
    moduleA();
    console.log(moduleA);
  }
  catch(err) {
    console.log(err);
  }
}

main();

moduleA.js

console.log('Inside global scope module A...');

function moduleA() {
  console.log('Running module A function...');
}

export default moduleA;

This is the result I'm getting when I run npx babel-node main.js

PS C:\myProject\test-module-imports> 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.<anonymous> (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)

.babelrc

{
  "presets": [
    [
      "@babel/preset-env", {
        "targets": {
          "node": "10"
        }
      }
    ]
  ]
}

I also tried this (the following code). And the result is the same:

main.js

let moduleA = null;
import('./moduleA').then((importedModule) => {
  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();

QUESTION

What am I doing wrong?


This is working fine (regular import): Executing also with npx babel-node src/main.js

import moduleA from './moduleA';

async function main() {
  try {
    console.log('Inside main function...');
    moduleA();
    console.log(moduleA);
  }
  catch(err) {
    console.log(err);
  }
}

main();

package.json (extra info)

{
  "name": "test-module-imports",
  "version": "1.0.0",
  "description": "",
  "main": "main.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && 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"
  }
}

The dynamic import() loads the module and return a module object that contains all its exports. In order to access the default export use the default property of the module object:

const moduleA = await import('./moduleA');
moduleA.default();

// OR

const { default: moduleA } = await import('./moduleA');
moduleA();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM