Esm: "TypeError: _181‍.t is not a constructor" with circular import dependency

Created on 24 Jun 2018  ·  6Comments  ·  Source: standard-things/esm

In converting some older code from using babel to using esm, I got this error:

TypeError: _181‍.t is not a constructor

It happens when calling new URL.URL(), and seems to be related to a circular import dependency; if I remove the circular import, then it proceeds okay.

Simplified reproducing repo at: https://github.com/willxy/esmtest

It does work with node --experimental-modules.

With esm it gives the error:

(function (require, module, __shared__) { "use strict";var __shared__;const e=module,t={Array:global.Array,Buffer:global.Buffer,Error:global.Error,EvalError:global.EvalError,Function:global.Function,JSON:global.JSON,Object:global.Object,Promise:global.Promise,RangeError:global.RangeError,ReferenceError:global.ReferenceError,SyntaxError:global.SyntaxError,TypeError:global.TypeError,URIError:global.URIError,eval:global.eval},r=global.console;module.exports=(function(e){var t={};function r(n){if(t[n])return t[n].exports;var i=t[n]={i:n,l:!1,exports:{}};return e[n].call(i.exports,i,i.exports,r),i.l=!0,i.exports}return r.d=function(e,t,r){Reflect.defineProperty(e,t,{configurable:!0,enumerable:!0,get:r})},r.n=function(e){return e.a=e},r.r=function(){},r(r.s=2)})([(function(e,t){var r;t=e.exports=K,"object"==typeof process&&process,r=function(){},t.SEMVER_SPEC_VERSION="2.0.0";var n=256,i=Number.MAX_SAFE_INTEGER||9007199254740991,s=t.re=[],a=t.src=[],o=0,l=o++;a[l]="0|

TypeError: _7a8‍.t is not a constructor
    at foo (/private/tmp/esmtest/a.mjs:15:12)
    at /private/tmp/esmtest/main.mjs:5:1
    at fl (/private/tmp/esmtest/node_modules/esm/esm.js:1:200282)
    at Object.run (/private/tmp/esmtest/node_modules/esm/esm.js:1:199615)
    at Object.<anonymous> (/private/tmp/esmtest/main.mjs:1:39)
    at Module.<anonymous> (/private/tmp/esmtest/node_modules/esm/esm.js:1:214444)
    at Zl (/private/tmp/esmtest/node_modules/esm/esm.js:1:208496)
    at /private/tmp/esmtest/node_modules/esm/esm.js:1:210055
    at au (/private/tmp/esmtest/node_modules/esm/esm.js:1:210291)
    at Module.g (/private/tmp/esmtest/node_modules/esm/esm.js:1:220036)

I'm including the code bits below for posterity.

Code bits

// index.js

'use strict';

require = require("esm")(module);   // eslint-disable-line no-global-assign
require("./main");
// main.mjs

import { foo } from './a';

foo();
// a.mjs

import URL from 'url';

import { g2 } from './b';

export function f1() {
    g2();
}

export function f2() {
}

export function foo() {
    let bar = new URL.URL("http://google.com");
    console.log("ok");
}
// b.mjs

import { f2 } from './a';

export function g1() {
    f2();
}

export function g2() {
}

bug

All 6 comments

Thanks @willxy!

Yep, I know where the issue is. Are you using the latest esm?

I think I'm testing with the latest -- looks I'm using 3.0.53.

Ah, yep. I can repro this. Fix coming.

Awesome, thanks!

I'll add a test in the AM.

Update:

Tests https://github.com/standard-things/esm/commit/5be62550d6f1de2f854edd072e1b32437b1033f3.

Update:

v3.0.54 is released 🎉

Confirmed fixed when tested with my older code too. Thank you!

Was this page helpful?
0 / 5 - 0 ratings