当前位置:   article > 正文

babel-plugin-component 按需引入组件时报错 Parsing error: Cannot find module “babel-preset-es2015“ 的解决办法_parsing error: cannot find module 'babel-preset-es

parsing error: cannot find module 'babel-preset-es2015' require stack:

安装 babel-plugin-component 插件

npm install babel-plugin-component -D

or

yarn add babel-plugin-component

在根目录(与 src 文件平级)的 .babelrc 文件(没有则创建,有则修改)中【可以合并到 babel.config.js 中,请往下看】

.babelrc

添加以下代码

  1. {
  2. "presets": [["es2015", { "modules": false }]],
  3. "plugins": [
  4. [
  5. "component",
  6. {
  7. "libraryName": "element-ui",
  8. "styleLibraryName": "theme-chalk"
  9. }
  10. ]
  11. ]
  12. }

报错信息:Parsing error: Cannot find module 'babel-preset-es2015'

解决办法:将 .babelrc 文件里面的 es2015 改为 @babel/preset-env 即可

  1. {
  2. "presets": [["@babel/preset-env", { "modules": false }]],
  3. "plugins": [
  4. [
  5. "component",
  6. {
  7. "libraryName": "element-ui",
  8. "styleLibraryName": "theme-chalk"
  9. }
  10. ]
  11. ]
  12. }

注意:.babelrc 的配置项,可以写入到 babel.config.js 文件中,之后不再需要 .babelrc 文件了。

因为 .babelrc 文件是旧版本的脚手架才有的,新版本是 babel.config.js 文件。

合并后的 babel.config.js 文件如下

  1. module.exports = {
  2. presets: [
  3. '@vue/cli-plugin-babel/preset',
  4. ["@babel/preset-env", { "modules": false }]
  5. ],
  6. "plugins": [
  7. [
  8. "component",
  9. {
  10. "libraryName": "element-ui",
  11. "styleLibraryName": "theme-chalk"
  12. }
  13. ]
  14. ]
  15. }

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号