power-assert icon indicating copy to clipboard operation
power-assert copied to clipboard

Cannot load module by SystemJS: Expected object

Open unlight opened this issue 9 years ago • 5 comments

version: 1.4.1 SystemJS settings:

map = {
      "power-assert": "n:power-assert",
};
packages = {
   "power-assert": { main: "build/power-assert.js", defaultExtension: "js" },
}

My code

import * as assert from 'power-assert';

Which transpiles to:

var assert = require('power-assert');

And this line throws error:

Error: (SystemJS) Expected object
    TypeError: Expected object

Stack trace:

systemjs.src.js ~ line:3326

    if (exports && (exports.__esModule || exports instanceof Module))
      entry.esModule = loader.newModule(exports); // line:3326 <--- exports = function powerAssert ()
    // set module as 'default' export, then fake named exports by iterating properties
    else if (entry.esmExports && exports !== __global)
      entry.esModule = loader.newModule(getESModule(exports));
    // just use the 'default' export
    else
      entry.esModule = loader.newModule({ 'default': exports });

systemjs.src.js ~ line:892

    // 26.3.3.12
    newModule: function (obj) {
      if (typeof obj != 'object')
        throw new TypeError('Expected object'); // line:892 <!--- obj  = function powerAssert (), typeof obj = "function"

      var m = new Module();

Link to 26.3.3.12 of Loader-Spec https://github.com/calvinmetcalf/Loader-Spec/blob/master/spec.md#263312-reflectloaderprototypenewmodule--obj-

obj is referencing to:

        powerAssert = function powerAssert () {
            return enhancement.apply(null, slice.apply(arguments));
        };

unlight avatar Sep 03 '16 16:09 unlight

Updated.

Commenting line: // define(defaultAssert, { '__esModule': true }); fixes the problem

unlight avatar Sep 03 '16 17:09 unlight

@unlight Thanks for your reporting and repro / fix case.

Hmm. Seems that your fix conflicts with ES6 Modules / CJS / Babel interop hack.

  • https://github.com/power-assert-js/power-assert/pull/28
  • https://github.com/power-assert-js/power-assert/pull/37

I know a little about SystemJS so I need your help more. Is there any solution in SystemJS to solve this situation?

twada avatar Sep 04 '16 11:09 twada

Hmm. Seems that your fix conflicts with ES6 Modules / CJS / Babel interop hack.

Maybe, but I just said that it fixes my problem (with SystemJS) I do not use Babel, etc.

I was going to fork, patch and test, but I couldn't. Because npm scripts is not for Windows. E.g.

mkdir -p ./build;
for i in $(find ./test/tobe_instrumented -name '*.js'); do babel $i > ./espowered_tests/tobe_instrumented/$(basename $i); done

unlight avatar Sep 04 '16 13:09 unlight

Do you have unit tests for #28 and #37? Actually #28 has no conflicts, it is the same code:

var defaultAssert = customize();
defaultAssert.default = defaultAssert;
module.exports = defaultAssert;
var defaultAssert = customize();
module.exports = defaultAssert;
module.exports['default'] = defaultAssert;

About #37. { '__esModule': true } I don't understand why it was added, this property is added by transpilers when generating from ES6 code to non-ES6. But your code written in es5.

I think @falsandtru should provide the case what cause him to pull such request. What module consumer/platform requires that.

unlight avatar Sep 04 '16 14:09 unlight

Would you try __useDefault: true?

Actually #28 has no conflicts, it is the same code:

ah, yes.

twada avatar Sep 05 '16 06:09 twada