63

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

khue bui
  • 1,366
  • 3
  • 22
  • 30

7 Answers7

141

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.

bingo
  • 2,298
  • 1
  • 15
  • 21
  • I had a similar error with wiredep, but was not able to figure out why it was retning thiis error, even with your provided code. I used something like this but still had a return of "First argument must be a string, Buffer, ArrayBuffer, Array..." gulp.task('wiredep', () => { gulp.src('app/styles/*.scss') .pipe(wiredep({ ignorePath: /^(\.\.\/)+/ }).on('error', function(e){ console.log(e); })) – Coded Container Aug 19 '16 at 18:31
  • Any advice on outputting the contained stacktrace a bit more readable? `JS_Parse_Error→stack:` and then it's all `\n\nmessa\nge...' and very hard to get for it... – Frank N Oct 31 '16 at 05:13
  • 1
    Write your log line like this if you don't want to break gulp `console.log(e.message); return this.end();` – Pablo S G Pacheco Jan 12 '17 at 19:39
  • It is better that 'uglify-js' can provide the details of error as default. – herbertD Aug 09 '18 at 07:36
17

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.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Senenko Vitaliy
  • 325
  • 3
  • 10
8

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

Yuval A.
  • 5,849
  • 11
  • 51
  • 63
4

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'));
  });
Kamuran Sönecek
  • 3,293
  • 2
  • 30
  • 57
0

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',
....
....
}
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
0

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.

Justin
  • 111
  • 1
  • 9
0

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

Blue
  • 22,608
  • 7
  • 62
  • 92
Ivanna
  • 1
  • 1