Cannot fix “Unexpected token name «i», expected punc «;»” from UglifyJs

前端 未结 3 1905
野趣味
野趣味 2021-01-18 23:56

I have created a react app with a custom next server

server.js:

const { createServer } = require(\'http\');
const next = require(\'n         


        
相关标签:
3条回答
  • 2021-01-19 00:32

    UglifyJs is ES5 compatible only (does not support ES6). I see in your code your first transpile ES6 to ES5 with Babel. Make sure Babel really transpiles ES5 to ES6 otherwise UglifyJs will throw this error !

    I had the same problem and what I did to fix the problem is the following: 1) Transpile ES6 js files with Babel and check the result. 2) As a result I saw that some part of the code was still in ES6! 3) Find the rootcause (my problem was a default babel file name that exclude node_module transpilation => babel 7 don't compile class ES6 which in node_modules).

    Hope it helps.

    0 讨论(0)
  • 2021-01-19 00:36

    You can use uglify-js-es6 npm i uglify-js-es6

    0 讨论(0)
  • 2021-01-19 00:47

    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.

    Instead of using UglifyJs, try using terser ! https://github.com/terser/terser

    Uglify-js is able to transpile only es5 syntax. If you want to transpile es6+ syntax use terser instead.

    0 讨论(0)
提交回复
热议问题