一、问题介绍:

在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 '#<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的方式编写即可

import {test} from './test';

export default test;

2、添加插件

 

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

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

{
  "plugins": ["@babel/plugin-transform-modules-commonjs"]
} 

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

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

Logo

前往低代码交流专区

更多推荐