Esm: ReferenceError in circular import: [ExportedName] is not defined

Created on 12 May 2018  ·  3Comments  ·  Source: standard-things/esm

Hi,

I have two files a.mjs and b.mjs

a.mjs

import { B } from './b';

export class A {
  static B() {
    return B;
  }
}

console.log(A);
console.log(B.A() === A);

b.mjs

import { A } from './a';

export class B {
  static A() {
    return A;
  }
}

When I run with node -r esm a.mjs, I got a ReferenceError: A is not defined

[Function: A]
<cwd>/b.mjs:5
  return B;

ReferenceError: A is not defined
    at Function.A (<cwd>/b.mjs:5:18)
    at <cwd>/a.mjs:10:27
    at Object.<anonymous> (<cwd>/a.mjs:1)

In contrast, when I run with node --experimental-modules a.mjs, everything goes fine

(node:27807) ExperimentalWarning: The ESM module loader is experimental.
[Function: A]
true

I believe the ReferenceError: A is not defined shouldn't have been thrown because A has got defined at the point of console.log(B.A() === A)

The error would not be thrown if I delay the call with process.nextTick

process.nextTick(() => {
  console.log(B.A() === A);
});

I also found that return B; is not at <cwd>/b.mjs:5, but at <cwd>/a.mjs:5

Here's the repro repo: esm-repro-issue-395

Thanks!

bug needs tests

Most helpful comment

@Mensu Yep! I force pushed an update.

Update:

v3.0.35 is released :tada:

All 3 comments

Thanks @Mensu!

@jdalton I tried e311e1c but found that there may be introduced a bug. Consider this modified a.mjs

import { B } from './b';

export const A = {}

console.log(B.A() === A);

which throws

<cwd>/a.mjs:3
       const A = {}_5a5‍.u(["A"]);
                   ^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:74:7)
    at Module.compile (<cwd>/esm/esm.js:9486:16)
    at tryCompileESM (<cwd>/esm/esm.js:8613:16)
    at tryCompileCached (<cwd>/esm/esm.js:8543:14)
    at compile (<cwd>/esm/esm.js:8500:12)
    at Module.compileWrapper (<cwd>/esm/esm.js:7399:81)
    at Module.child._compile (<cwd>/esm/esm.js:9023:22)
    at Object.methodWrapper (<cwd>/esm/esm.js:7432:11)
    at Object.managerWrapper (<cwd>/esm/esm.js:7360:30)
    at Object.<anonymous> (<cwd>/esm/esm.js:21163:24)

the generated code is something like

...
       const A = {}_5a5‍.u(["A"]);

console.log(_5a5‍.t("B",B.A()) === A);
...

This should be related to the two calls of this.magicString.prependRight

@Mensu Yep! I force pushed an update.

Update:

v3.0.35 is released :tada:

Was this page helpful?
0 / 5 - 0 ratings