laravel-mix: UglifyJs: Unexpected token: operator (>)

When running npm run production I get the following error:

/js/listing.js from UglifyJs
Unexpected token: operator (>) [./~/bootstrap/js/src/dropdown.js:11,0][/js/listing.js:3302,23]

I’ve tried deleting node_modules without any luck, same goes for installing:

yarn add --dev git+https://github.com/mishoo/UglifyJS2.git#harmony

Arrow function seem to be the issue, line 11 of dropdown.js is:

const Dropdown = (($) => {

I don’t have a .babelrc file.

laravel-mix@0.11.4
Node: v7.9.0
NPM: 4.5.0
macOS: 10.12.4

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 25
  • Comments: 46 (10 by maintainers)

Most upvoted comments

Same problem.

For webpack v3 onwards the aliasing for UglifyJS is changed. Refer https://github.com/webpack-contrib/uglifyjs-webpack-plugin.

So for me modifying my webpack.config.js from to use new UglifyJsPlugin() rather than new webpack.optimize.UglifyJsPlugin() fixed the issue.

Reference: https://github.com/joeeames/WebpackFundamentalsCourse/issues/3#issuecomment-352541826

@JeffreyWay: Could you re-open this until it’s resolved?

My babel ES2015 preset was excluding /node_modules/ and this would end up not converting some newer modules before uglify saw the code and it choked on the arrow function like the OP title, adding node_modules back solved the issue here (Webpack)

    {
        test: /\.js$/,
        loader: 'babel-loader',
        query: {
          presets: ["es2015"],
        },
        exclude: /node_modules/ <-- REMOVE THIS
      }, 

@ipa1981 rm -rf node_modules directory, rm package-lock.json and npm install. See if that works.

When upgrading from 0.* to 1.* I had to do that to make things work.

Also, check if your npm is now 5.0.4. 5.0.3 had problems with some packages/dependencies not being installed.

Shouldn’t uglify work out of the box, since it’s enabled by default in production?

@ruchern, thanks for ideas! I just tried many different things. And it seems CommonJS syntax:

Vue.component('sidebar', require('vue-strap').aside)

finally works with latest vue-loader 13.0.0 (I had 12.2.1 on the time testing).

So at the moment it looks like everything is fine - both watch and production scripts compiles.

@ruchern thanks a lot, seems this works! Not exactly as you proposed, but in original app.js file I had to change this:

Vue.component('alert', require('../../../node_modules/vue-strap/src/Alert.vue'))

to this

Vue.component('alert', require('vue-strap').alert)

I don’t know what’s the difference however, but this goes to compile.

@iraklisg Nope. upgraded to 1.0.7, still the same:

 ERROR  Failed to compile with 1 errors                              11:59:05 AM

 error  

/js/app.js from UglifyJs
Unexpected token: operator (>) [./node_modules/vue-strap/src/utils/utils.js:4,0][/js/app.js:28274,16]

I’m about to tag a new release that bumps the Uglify version, so I think that should fix this.

Getting the same when using a package that has => {}. Seems like UglifyJs is working on it https://github.com/mishoo/UglifyJS2/issues/448

I’m still getting errors for shorthand closures.

@JeffreyWay perhaps swap from JSUglify to https://github.com/babel/babili ? Babili supports ES6

@ipa1981 I have fixed your issue.

In your app.js file,

Use this instead of anything else.

import {utils} from "vue-strap";

asciicast

@balthild, all upgrading to laravel-mix@1.0.7 solved the problem for me