当先锋百科网

首页 1 2 3 4 5 6 7

ERROR in xxx.js from UglifyJs

问题

ERROR in app.bundle.js from UglifyJs
Unexpected token: name «element», expected: punc «;» [app.bundle.js:106,8]

在用webpack对代码打包的时候报错,在网上找到了类似的答案,还是没有解决我的问题.

能解决大部分问题的链接

https://stackoverflow.com/questions/41254538/error-in-bundle-js-from-uglifyjs
http://www.cnblogs.com/aredleave/p/7586911.html
https://segmentfault.com/a/1190000011212544

按照以上方法对环境进行重新配置,依然报错!

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module '@babel/core' babel-loader@8 requires Babel 7.x (the package '@babel/core').
If you'd like to use Babel 6.x ('babel-core'), you should install 'babel-loader@7'.

报错原因

通过代码报错原因是 babel-loader 版本问题,因为 babel-loader 用的最新的版本,和 UglifyJs 不兼容.

解决方法

通过运行下面代码对babel-loader进行重新安装(低版本).

npm install --save-dev babel-loader@7

通过修改后的package.json配置文件,亲测可以正确打包,无报错现象.

{
  "name": "kankan",
  "version": "1.0.0",
  "description": "kankan",
  "main": "webpack.config.js",
  "scripts": {
    "start": "webpack-dev-server --open --config webpack.dev.js",
    "build": "webpack --config webpack.prod.js"
  },
  "author": "kankan",
  "license": "ISC",
  "devDependencies": {
    "babel": "^6.23.0",
    "babel-core": "^6.26.3",
    "babel-loader": "^7.1.5",
    "babel-preset-es2015": "^6.24.1",
    "clean-webpack-plugin": "^2.0.1",
    "css-loader": "^2.1.1",
    "express": "^4.16.4",
    "html-webpack-plugin": "^3.2.0",
    "style-loader": "^0.23.1",
    "uglifyjs-webpack-plugin": "^2.1.2",
    "webpack": "^4.30.0",
    "webpack-cli": "^3.3.1",
    "webpack-dev-middleware": "^3.6.2",
    "webpack-dev-server": "^3.3.1",
    "webpack-manifest-plugin": "^2.0.4",
    "webpack-merge": "^4.2.1"
  }
}

奇卡奇卡 哈哈哈

个人主页:https://kankan.fun

明天的你一定会感谢现在拼命的自己!