GulpUglifyError:Unable to minify JavaScript

Solution 1:

The main error is generated when you're using ES6 format. Use the gulp-uglify-es module instead of 'gulp-uglify' to overcome this error.

var uglify = require('gulp-uglify-es').default;

Note: gulp-uglify-es is no longer being maintained. You may want to use terser/gulp-terser instead:

Solution 2:

To see the error in console:

var gutil = require('gulp-util');

gulp.task('concat', function() {
return gulp.src('app/**/*.js')
    // .pipe(concat('script.js'))
    .pipe(uglify())
    .on('error', function (err) { gutil.log(gutil.colors.red('[Error]'), err.toString()); })
    .pipe(gulp.dest('./dist/'))
});

To find the exact file, with line number of error register and run this task:

var pump = require('pump');

gulp.task('uglify-error-debugging', function (cb) {
  pump([
    gulp.src('app/**/*.js'),
    uglify(),
    gulp.dest('./dist/')
  ], cb);
});

Solution 3:

I think the top answers here are not explaining how to get the error. The docs have a section on error handling:

gulp-uglify emits an 'error' event if it is unable to minify a specific file

So, just capture the error and do whatever you want with it (such as logging to console) to see the filename, line number, and additional info:

uglify().on('error', console.error)

or in a larger context:

gulp.task('foo', () => {

    return gulp.src([
        'asset/src/js/foo/*.js',
        'asset/src/js/bar/*.js',
    ])
    .pipe(uglify().on('error', console.error))
    .pipe(concat('bundle.js'))
    .pipe(gulp.dest('./'));

});

This gives you a super helpful error!

{ GulpUglifyError: unable to minify JavaScript
    at [stack trace])
  cause:
   { SyntaxError: Continue not inside a loop or switch
       [stack trace]
     message: 'Continue not inside a loop or switch',
     filename: 'ProductForm.js',
     line: 301,
     col: 37,
     pos: 10331 },
  plugin: 'gulp-uglify',
  fileName:
   '/asset/src/js/foo/ProductForm.js',
  showStack: false }