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';
export let foo = 'foo';
// b.mjs

import {foo} from './a';
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
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
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

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.