khue bui
khue bui

Reputation: 1416

Gulp-uglify : throw er; // Unhandled 'error' event

i'm pretty new to gulp , and I follow tutorials in http://leveluptuts.com/tutorials/learning-gulp , I get this error:

events.js:141
  throw er; // Unhandled 'error' event
  ^
Error
at new JS_Parse_Error (/home/kid/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:196:18)
at js_error (/home/kid/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:204:11)
at croak (/home/kid/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:680:9)
at token_error (/home/kid/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:688:9)
at unexpected (/home/kid/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:694:9)
at expr_atom (/home/kid/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1201:9)
at maybe_unary (/home/kid/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1363:19)
at expr_ops (/home/kid/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1398:24)
at maybe_conditional (/home/kid/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1403:20)
at maybe_assign (/home/kid/node_modules/gulp-uglify/node_modules/uglify-js/lib/parse.js:1427:20)

here is my code :

 var gulp = require('gulp')
    uglify = require('gulp-uglify');

gulp.task('default', function() {
    // body...
    gulp.src('js/*.js')
    .pipe(uglify())
    .pipe(gulp.dest('minjs'));

});

and the tree of directory, just simple

-gulp
     -js/
     -gulpfile.js

thanks a lot

Upvotes: 63

Views: 56151

Answers (7)

Ivanna
Ivanna

Reputation: 1

use gulp-uglify-es instead gulp-uglify. It works with es6

Upvotes: 0

Yuval A.
Yuval A.

Reputation: 6089

In my case, it seems it doesn't accept "arrow-syntax" function (e.g. data=>{ // do something; })

Upvotes: 8

Justin
Justin

Reputation: 111

I had the same issue and it came down to a js file that was giving out issues. The biggest issue was I had 10 js files but after a little digging my issue was not adding ; since this will minify your code it does not matter you use ES6 you need to add ; at the end of your code or minifying won't work.

Upvotes: 0

Senenko Vitaliy
Senenko Vitaliy

Reputation: 325

I had the same error. So I tried to output an error to the console (thanks to bingo). I realized that the problem is that gulp-uglify doesn't want to work with ES6. I changed my JS code to ES2015 and voila. You can also use gulp-babel.

Upvotes: 17

Kamuran Sönecek
Kamuran Sönecek

Reputation: 3333

Also you can use gulp-util.

https://www.npmjs.com/package/gulp-util

var gutil = require('gulp-util');
gulp.task('scripts', ['clean'], function () {
      return gulp.src('js/*.js')
        .pipe(uglify().on('error',gutil.log))
        .pipe(gulp.dest('minjs'));
  });

Upvotes: 4

Himanshu Upadhyay
Himanshu Upadhyay

Reputation: 6565

I had the same issue and was getting the same error. The problem was, one of my JS files had @charset "UTF-8"; in the first line. So the syntax was breaking due to @ symbol. I removed it and it worked well.

{
SyntaxError: Unexpected character '@' at JS_Parse_Error.get 
(eval at <anonymous> (C:\xampp\htdocs\catch\node_moles\uglify-js\tools\node.js:21:1),
<anonymous>:86:23) at formatError (util.js:649:15)
at formatValue (util.js:554:18)
at formatProperty (util.js:795:15)
at util.js:655:12
at Array.map (native)
at formatObject (util.js:654:15)
at formatValue (util.js:593:16)
at inspect (util.js:186:10)
at exports.format (util.js:72:24)
message: 'Unexpected character \'@\'',
filename: 'all.min.css', line: 1, col: 0, pos: 0 },
plugin: 'gulp-uglify',
....
....
}

Upvotes: 0

bingo
bingo

Reputation: 2308

Your uglify task is probably choking on one of the files it is trying to process. Handle the error and write the output to the console so you can see which file causing the task to fail.

 gulp.task('scripts', ['clean'], function () {
      return gulp.src('js/*.js')
        .pipe(uglify().on('error', function(e){
            console.log(e);
         }))
        .pipe(gulp.dest('minjs'));
  });

When you run your gulp task again, you will still get an error, but this time, right towards the top of the output, you will see the file and line number that uglify is having trouble processing. Debug from there.

Maybe its a syntax error? Fix it and try again.

Maybe you've got a weird _reference.js file with unexpected characters like those you see in Visual Studio projects sometimes? Exclude it from the gulp.src and try again.

Upvotes: 141

Related Questions