Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

gulp-uglify

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

like image 481
khue bui Avatar asked Oct 02 '15 15:10

khue bui


Video Answer


2 Answers

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.

like image 120
bingo Avatar answered Sep 22 '22 02:09

bingo


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.

like image 43
Senenko Vitaliy Avatar answered Sep 22 '22 02:09

Senenko Vitaliy