Cannot assign to read only property exports of object

太过爱你忘了你带给我的痛 2023-10-07 08:54 158阅读 0赞

一、问题介绍:

在webpack项目中使用@babel/plugin-transform-runtime时,报错

ERROR in Error: E:\workdata\webpackVue/node_modules/_@babel_runtime@7.4.5@@babel/runtime/helpers/ typeof.js?:33
module.exports = _typeof;
^
TypeError: Cannot assign to read only property ‘exports’ of object ‘#
- typeof.js?:33 Module.eval
[.]/[_@babel_runtime@7.4.5@@babel]/runtime/helpers/typeof.js?:33:16

- typeof.js?:34 eval

在网上查了一下,具体原因是项目中文件存在语法冲突,

就是在webpack打包的时候,可以在js文件中混用require和export。但是不能混用import 以及module.exports。

因为webpack中不允许混用import和module.exports,

二、解决方案:

1、统一改成ES6的方式编写即可

  1. import {test} from './test';
  2. export default test;

2、添加插件

  1. npm install --save-dev @babel/plugin-transform-modules-commonjs

在项目根目录新增.babelrc文件,并在文件中加入

  1. {
  2. "plugins": ["@babel/plugin-transform-modules-commonjs"]
  3. }

然后重新启动项目就OK了。

它类似一种打补丁,会把项目中需要转换语法的文件转换。

发表评论

表情:
评论列表 (有 0 条评论,158人围观)

还没有评论,来说两句吧...

相关阅读