GulpUglifyError: unable to minify JavaScript

Raymond Tang Raymond Tang 0 3179 2.13 index 5/28/2021

Issue context

When using Gulp Uglify to minimize JavaScript script files, the following error is throw out:

GulpUglifyError: unable to minify JavaScriptCaused by: SyntaxError: Unexpected token: keyword «const

The root cause is that Uglify is not up to date and doesn't support es6. For this case, const keyword is not supported.

Resolution

One workaround is to replace Ugnify with gulp-terser.

Follow these steps to do that:

  1. Install gulp-terser by updating package.json or use npm commands.

      "devDependencies": {
        "gulp": "^4.0.0",
        "gulp-concat": "2.6.1",
        "gulp-cssmin": "0.2.0",
        "rimraf": "2.6.1",
        "gulp-sass": "4.0.2",
        "gulp-google-webfonts": "4.0.0",
        "gulp-terser": "2.0.1"
      }
    

    Npm commands:

    npm install gulp-terser --save-dev
    
  2. Replace uglify function with terser:

    const uglify = require("gulp-terser");
    

    With this approach, you don't need to replace function uglify.

gulp javascript node.js

Join the Discussion

View or add your thoughts below

Comments