【问题标题】:Weird error with CommonJs under Webpack4 + babel8 + coreJs@3Webpack 4 + babel + core Js@3 下的 CommonJs 出现奇怪错误
【发布时间】:2020-06-29 04:21:33
【问题描述】:

这是入口:

// index.js
const {test} = require('./handle')
test()

这是一个commonjs模块

// handle.js
module.exports = {
  test() {
    console.log('>>>>>   handle get trigger')
    console.log(typeof [])
  }
}

编译成功,然后运行时出现这个错误

Uncaught TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

如果我从handle.js 中删除console.log(typeof []),错误就会消失。

我的 babel 配置是:

{
                test: /\.jsx?$/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ["@babel/preset-env"],
                        plugins: [
                            [
                                "@babel/plugin-transform-runtime",
                                {
                                    "corejs": 3
                                }
                            ],
                            "@babel/plugin-syntax-dynamic-import"
                        ]
                    }
                },
                include: /src/
            },

当我使用 ES6 模块编写此代码时,错误也消失了。

【问题讨论】:

    标签: webpack babeljs commonjs


    【解决方案1】:

    @babel/plugin-transform-runtimeimport 添加到我的文件中! 所以它变成了 ES6Module 和 CommonJs 的混合使用! 导致问题!!!!!!

    所以,我们只能使用 Es6module 或者添加一个 babel 插件@babel/plugin-transform-modules-commonjs

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 2020-08-18
      • 1970-01-01
      • 1970-01-01
      • 2017-05-10
      • 1970-01-01
      相关资源
      最近更新 更多