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

I'm new to gulp and I follow the tutorials at 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 directory tree, just

 -gulp -js/ -gulpfile.js 

Thank you very much

+44
gulp-uglify
Oct 02 '15 at 15:59
source share
5 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 that you can see which file is causing the task.

  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 the gulp task again, you will still receive an error message, but this time, right to the top of the output, you will see the file and line number that uglify has processing problems. Debugging from there.

Maybe this is a syntax error? Correct it and try again.

Perhaps you have a strange _reference.js file with unexpected characters similar to the ones you see in Visual Studio projects? Exclude it from gulp.src and try again.

+95
08 Oct '15 at 3:39 on
source share

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

+4
Jul 13. '17 at 18:06
source share

You can also 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')); }); 
+2
Jun 21 '17 at 10:13
source share

I had the same problem and was getting the same error. The problem was that one of my JS files had @charset "UTF-8"; in the first line. Thus, the syntax was interrupted due to the @ character. I deleted 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', .... .... } 
0
Jun 05 '17 at 12:57 on
source share

I had the same problem and it ended up in a js file that was showing problems. The biggest problem was that I had 10 js files, but after a bit of copying I did not add ; since this will reduce your code, it doesn’t matter that you are using ES6, you need to add ; at the end of your code, either will not work.

0
Dec 18 '17 at 10:59 on
source share



All Articles