无法从 UglifyJs 修复 "Unexpected token name «i», expected punc «;»"
Cannot fix "Unexpected token name «i», expected punc «;»" from UglifyJs
我创建了一个带有自定义下一个服务器的 React 应用程序
server.js:
const { createServer } = require('http');
const next = require('next');
const app = next({
dev: process.env.NODE_ENV !== 'production',
conf: {
webpack: config => {
config.devtool = false;
for (const r of config.module.rules) {
if (r.loader === 'babel-loader') {
r.options.sourceMaps = false;
}
}
return config;
}
}
});
const routes = require('./routes');
const handler = routes.getRequestHandler(app);
app.prepare().then(() => {
createServer(handler).listen(3000, err => {
if (err) throw err;
});
});
但是,我在使用 npm 运行 build 时遇到问题,因为我得到以下 error:
Unexpected token name «i», expected punc «;» [commons.js:124406,11]
at /home/parstoo/Dropbox/Projects/Ethereum/SupplyChain/node_modules/next/dist/server/build/index.js:182:21
根据论坛的说法,问题是因为UglifyJs不支持ES6,所以我尝试通过这些链接解决它:this and 。我几乎尝试了所有建议,但其中 none 行得通。
另外,我的根目录下没有webpack.config.js。所以,我用这个内容在根目录中删除了一个(我不知道它是否正确):
webpack.config.js:
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
optimization: {
minimizer: [new UglifyJsPlugin()],
},
};
package.json内容:
{
"name": "supplychain",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha",
"dev": "node server.js",
"start": "NODE_ENV=production server.js",
"transpile": "babel src -d dist --copy-files",
"prepublishOnly": "npm run transpile",
"build": "next build",
"deploy": "gh-pages -d examples/dist",
},
"author": "",
"license": "ISC",
"dependencies": {
"@babel/polyfill": "^7.2.5",
"fs-extra": "^7.0.1",
"ganache-cli": "^6.4.1",
"mocha": "^5.2.0",
"next": "^4.1.1",
"next-routes": "^1.4.2",
"radium": "^0.25.1",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"semantic-ui-react": "^0.82.5",
"solc": "^0.4.25",
"truffle-hdwallet-provider": "0.0.3",
"web3": "^1.0.0-beta.35"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"css-loader": "^2.1.0",
"html-webpack-plugin": "^3.2.0",
"npm-install-webpack-plugin": "^4.0.5",
"terser-webpack-plugin": "^1.3.0",
"uglifyjs-webpack-plugin": "v1.0.0-beta.1",
"webpack": "^4.35.0",
"webpack-cli": "^3.3.4",
"webpack-dev-server": "^3.7.2"
}
}
有人可以帮我解决这个问题吗?
According to forums, the problem is caused because UglifyJs does not
support ES6 so I tried to solve it with these links: this and this. I
almost tried all the suggestion but none of them worked.
不要使用 UglifyJs,请尝试使用 terser! https://github.com/terser/terser
Uglify-js 只能转译 es5 语法。如果你想转译 es6+ 语法,请改用 terser。
UglifyJs 仅兼容 ES5(不支持 ES6)。我在你的代码中看到你第一次使用 Babel 将 ES6 转换为 ES5。确保 Babel 确实将 ES5 转换为 ES6,否则 UglifyJs 将抛出此错误!
我遇到了同样的问题,我是如何解决这个问题的:
1)用Babel转译ES6 js文件并查看结果。
2)结果看到部分代码还是ES6的!
3) 找到根本原因(我的问题是默认的 babel 文件名排除了 node_module 转译 => )。
希望对您有所帮助。
你可以使用uglify-js-es6
npm i uglify-js-es6
可能是一个相当小众的案例,但我在调用 await 时没有将函数声明为异步时遇到此错误:
function doThing() {
...
let response = await fetch('url');
...
}
这给了我以下错误:
Caused by: SyntaxError: Unexpected token: name «fetch», expected: punc «;»
虽然错误是正确的,但对解决问题毫无帮助。解决方法是在函数声明之前添加异步。
我创建了一个带有自定义下一个服务器的 React 应用程序
server.js:
const { createServer } = require('http');
const next = require('next');
const app = next({
dev: process.env.NODE_ENV !== 'production',
conf: {
webpack: config => {
config.devtool = false;
for (const r of config.module.rules) {
if (r.loader === 'babel-loader') {
r.options.sourceMaps = false;
}
}
return config;
}
}
});
const routes = require('./routes');
const handler = routes.getRequestHandler(app);
app.prepare().then(() => {
createServer(handler).listen(3000, err => {
if (err) throw err;
});
});
但是,我在使用 npm 运行 build 时遇到问题,因为我得到以下 error:
Unexpected token name «i», expected punc «;» [commons.js:124406,11]
at /home/parstoo/Dropbox/Projects/Ethereum/SupplyChain/node_modules/next/dist/server/build/index.js:182:21
根据论坛的说法,问题是因为UglifyJs不支持ES6,所以我尝试通过这些链接解决它:this and
另外,我的根目录下没有webpack.config.js。所以,我用这个内容在根目录中删除了一个(我不知道它是否正确): webpack.config.js:
const path = require('path');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
module.exports = {
optimization: {
minimizer: [new UglifyJsPlugin()],
},
};
package.json内容:
{
"name": "supplychain",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "mocha",
"dev": "node server.js",
"start": "NODE_ENV=production server.js",
"transpile": "babel src -d dist --copy-files",
"prepublishOnly": "npm run transpile",
"build": "next build",
"deploy": "gh-pages -d examples/dist",
},
"author": "",
"license": "ISC",
"dependencies": {
"@babel/polyfill": "^7.2.5",
"fs-extra": "^7.0.1",
"ganache-cli": "^6.4.1",
"mocha": "^5.2.0",
"next": "^4.1.1",
"next-routes": "^1.4.2",
"radium": "^0.25.1",
"react": "^16.8.4",
"react-dom": "^16.8.4",
"semantic-ui-react": "^0.82.5",
"solc": "^0.4.25",
"truffle-hdwallet-provider": "0.0.3",
"web3": "^1.0.0-beta.35"
},
"devDependencies": {
"@babel/cli": "^7.2.3",
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"babel": "^6.23.0",
"babel-cli": "^6.26.0",
"babel-preset-es2015": "^6.24.1",
"css-loader": "^2.1.0",
"html-webpack-plugin": "^3.2.0",
"npm-install-webpack-plugin": "^4.0.5",
"terser-webpack-plugin": "^1.3.0",
"uglifyjs-webpack-plugin": "v1.0.0-beta.1",
"webpack": "^4.35.0",
"webpack-cli": "^3.3.4",
"webpack-dev-server": "^3.7.2"
}
}
有人可以帮我解决这个问题吗?
According to forums, the problem is caused because UglifyJs does not support ES6 so I tried to solve it with these links: this and this. I almost tried all the suggestion but none of them worked.
不要使用 UglifyJs,请尝试使用 terser! https://github.com/terser/terser
Uglify-js 只能转译 es5 语法。如果你想转译 es6+ 语法,请改用 terser。
UglifyJs 仅兼容 ES5(不支持 ES6)。我在你的代码中看到你第一次使用 Babel 将 ES6 转换为 ES5。确保 Babel 确实将 ES5 转换为 ES6,否则 UglifyJs 将抛出此错误!
我遇到了同样的问题,我是如何解决这个问题的:
1)用Babel转译ES6 js文件并查看结果。
2)结果看到部分代码还是ES6的!
3) 找到根本原因(我的问题是默认的 babel 文件名排除了 node_module 转译 =>
希望对您有所帮助。
你可以使用uglify-js-es6 npm i uglify-js-es6
可能是一个相当小众的案例,但我在调用 await 时没有将函数声明为异步时遇到此错误:
function doThing() {
...
let response = await fetch('url');
...
}
这给了我以下错误:
Caused by: SyntaxError: Unexpected token: name «fetch», expected: punc «;»
虽然错误是正确的,但对解决问题毫无帮助。解决方法是在函数声明之前添加异步。