Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"   } } 
like image 354
cbdeveloper Avatar asked Nov 14 '19 14:11

cbdeveloper


People also ask

How do I import a dynamic module in NodeJS?

To load dynamically a module call import(path) as a function with an argument indicating the specifier (aka path) to a module. const module = await import(path) returns a promise that resolves to an object containing the components of the imported module. } = await import(path);

What is a dynamic import ()?

The import() call, commonly called dynamic import, is a function-like expression that allows loading an ECMAScript module asynchronously and dynamically into a potentially non-module environment.

How do I import a function in NodeJS?

To include functions defined in another file in Node. js, we need to import the module. we will use the require keyword at the top of the file. The result of require is then stored in a variable which is used to invoke the functions using the dot notation.

Can I use import in NodeJS?

Node js doesn't support ES6 import directly. If we try to use import for importing modules directly in node js it will throw out the error. For example, if we try to import express module by writing import express from 'express' node js will throw an error as follows: Node has experimental support for ES modules.


1 Answers

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(); 
like image 153
Fraction Avatar answered Sep 30 '22 10:09

Fraction