undefinedfix
Sign in

Why does the variable of import loop load module display undefined instead of not defined?

Domski edited in Sun, 19 Jun 2022

Ruan Yifeng's course: http://es6.ruanyifeng.com/#do...

// a.mjs

import {bar} from './b';
console.log('a.mjs');
console.log(bar);
export let foo = 'foo';
// b.mjs

import {foo} from './a';
console.log('b.mjs');
console.log(foo);
export let bar = 'bar';

In the above code, a.mjs loads b.mjs, and b.mjs loads a.mjs, forming a cyclic load. Execute a.mjs, and the result is as follows.

$ node --experimental-modules a.mjs
b.mjs
ReferenceError: foo is not defined

The above is the content of the tutorial.

I'm undefined in the browser environment. Isn't let upgraded without variable declaration? Why is it undefined instead of not defined? I have two guesses, but I'm not sure. Please give me some advice

2 Replies
rortr
commented on Mon, 20 Jun 2022

Did you change the content of the demo? I can directly copy the example to test, and the ES module environment goes deep into commonjs and ES6 module

d3vCr0w
commented on Mon, 20 Jun 2022

It has nothing to do with variable promotion. ES6 is a dynamic reference. It is a common JS specification in node environment.

lock This question has been locked and the reply function has been disabled.