Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

`@babel/runtime-corejs3` causes `TypeError: Cannot assign to read only property 'exports' of object '#<Object>'` with some import statements. #10792

Closed
mizchi opened this issue Dec 2, 2019 · 6 comments

Comments

@mizchi
Copy link

@mizchi mizchi commented Dec 2, 2019

Bug Report

Current Behavior

@babel/runtime-corejs3 causes runtime error on webpack build: TypeError: Cannot assign to read only property 'exports' of object '#<Object>' with some import statements.

Input Code

// src/index.js
import { ok } from "assert"; // no error when I erase this import

async function foo() {
  return 1;
}

async function run() {
  const ret = await foo();
  ok(ret === 1);
}

run();

I tried some import patterns.

  • import { ok } from "assert" : error
  • import dep from "./dep";: It works
  • import flatten from "lodash-es/flatten": It works.

I doubt nodejs's util and runtime-corejs3 are bad synergy.

Expected behavior/code

No Error on runtime

Babel Configuration (babel.config.js, .babelrc, package.json#babel, cli command, .eslintrc)

// babel.config.js
module.exports = {
  presets: [
    [
      "@babel/preset-env",
      {
        modules: false,
        useBuiltIns: "usage",
        corejs: 3
      }
    ]
  ],
  plugins: [["@babel/transform-runtime", { corejs: 3, regenerator: true }]]
};

webpack --mode development and node dist/main.js

/Users/mizchi/plg/aalegacy/dist/main.js:7029
  module.exports = function inherits(ctor, superCtor) {
                 ^

TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
    at Module.<anonymous> (/Users/mizchi/plg/aalegacy/dist/main.js:7029:18)
    at Module../node_modules/util/node_modules/inherits/inherits_browser.js (/Users/mizchi/plg/aalegacy/dist/main.js:7052:30)
    at __webpack_require__ (/Users/mizchi/plg/aalegacy/dist/main.js:20:30)
    at Object.<anonymous> (/Users/mizchi/plg/aalegacy/dist/main.js:7658:20)
    at Object../node_modules/util/util.js (/Users/mizchi/plg/aalegacy/dist/main.js:7783:30)
    at __webpack_require__ (/Users/mizchi/plg/aalegacy/dist/main.js:20:30)
    at Object.<anonymous> (/Users/mizchi/plg/aalegacy/dist/main.js:203:12)
    at Object../node_modules/assert/assert.js (/Users/mizchi/plg/aalegacy/dist/main.js:638:30)
    at __webpack_require__ (/Users/mizchi/plg/aalegacy/dist/main.js:20:30)
    at Module../src/index.ts (/Users/mizchi/plg/aalegacy/dist/main.js:7872:64)

That code is around here.

// ...
if (typeof _babel_runtime_corejs3_core_js_stable_object_create__WEBPACK_IMPORTED_MODULE_0___default.a === 'function') {
  // implementation from standard node.js 'util' module
  module.exports = function inherits(ctor, superCtor) {
    ctor.super_ = superCtor;
    ctor.prototype = _babel_runtime_corejs3_core_js_stable_object_create__WEBPACK_IMPORTED_MODULE_0___default()(superCtor.prototype, {
      constructor: {
        value: ctor,
        enumerable: false,
        writable: true,
        configurable: true
      }
    });
  };
} else {
  // old school shim for old browsers
  module.exports = function inherits(ctor, superCtor) {
    ctor.super_ = superCtor;

    var TempCtor = function TempCtor() {};

    TempCtor.prototype = superCtor.prototype;
    ctor.prototype = new TempCtor();
    ctor.prototype.constructor = ctor;
  };
}

Environment

  System:
    OS: macOS Mojave 10.14.5
  Binaries:
    Node: 12.10.0 - ~/.nodenv/versions/12.10.0/bin/node
    Yarn: 1.19.1 - ~/.nodenv/versions/12.10.0/bin/yarn
    npm: 6.13.0 - ~/.nodenv/versions/12.10.0/bin/npm
  npmPackages:
    @babel/core: ^7.7.4 => 7.7.4 
    @babel/plugin-transform-runtime: ^7.7.4 => 7.7.4 
    @babel/preset-env: ^7.7.4 => 7.7.4 
    @babel/preset-typescript: ^7.7.4 => 7.7.4 
    @babel/runtime: ^7.7.4 => 7.7.4 
    @babel/runtime-corejs3: ^7.7.4 => 7.7.4 
    babel-loader: ^8.0.6 => 8.0.6 
    webpack: ^4.41.2 => 4.41.2 

I doubt babel/preset-typescript at first but there is no effect about this case.

Possible Solution

Additional context/Screenshots

@babel-bot

This comment has been minimized.

Copy link
Collaborator

@babel-bot babel-bot commented Dec 2, 2019

Hey @mizchi! We really appreciate you taking the time to report an issue. The collaborators on this project attempt to help as many people as possible, but we're a limited number of volunteers, so it's possible this won't be addressed swiftly.

If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack community that typically always has someone willing to help. You can sign-up here for an invite."

@JLHwung

This comment has been minimized.

Copy link
Contributor

@JLHwung JLHwung commented Dec 2, 2019

at webpack_require (/Users/mizchi/plg/aalegacy/dist/main.js:20:30)

The error stack reveals that webpack generates __webpack_require__ in the output bundle. If you meant to bundle an app and run in node.js, consider set target to "node" so that webpack will generate bundles that can leverage node.js require.

@mizchi

This comment has been minimized.

Copy link
Author

@mizchi mizchi commented Dec 3, 2019

@JLHwung In this case(node dist/main.js), it works. thx!
But, sorry, I want to show you simple case. I want to transform for IE11 indeed.

webpack default assert polyfill looks broken yet with this case in browser.

@mizchi

This comment has been minimized.

Copy link
Author

@mizchi mizchi commented Dec 3, 2019

This is real log on github CI (IE on Windows 10 Container) https://github.com/mizchi-sandbox/aalegacy/pull/1/checks

@loganfsmyth

This comment has been minimized.

Copy link
Member

@loganfsmyth loganfsmyth commented Dec 3, 2019

Can you try adding sourceType: "unambiguous" to your config?

// babel.config.js
module.exports = {
  sourceType: "unambiguous",
  presets: [
@mizchi

This comment has been minimized.

Copy link
Author

@mizchi mizchi commented Dec 4, 2019

It works! https://github.com/mizchi-sandbox/aalegacy/pull/1/checks
I resolved this issue (but I don't know why. need some documentation?)

@mizchi mizchi closed this Dec 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.