Uglifyjs: JS_Parse_Error doesn't give any useful stack trace or information

Created on 13 Nov 2013  ·  45Comments  ·  Source: mishoo/UglifyJS

This stack overflow question describes this problem well enough: http://stackoverflow.com/questions/17951602/how-to-find-source-of-js-parse-error

I have the same problem - it tells me there's an error, but none of the stack trace is my code, and it doesn't say what its problem is.

Most helpful comment

If your sanity is on the line, you probably shouldn't be using gulp.

All 45 comments

Which version are you using? (Just to clarify, this problem was fixed in a89d2333 (2 months ago))

Ah, I was using 2.4.0. I tried 2.4.3 (i think thats the newest version) with the same problem tho.

Here's my specific stack trace:

      Exception: Error
          at new JS_Parse_Error (F:\<omitted>\node_modules\uglify-js\lib\parse.js:1
96:18)
          at js_error (F:\<omitted>\node_modules\uglify-js\lib\parse.js:204:11)
          at croak (F:\<omitted>\node_modules\uglify-js\lib\parse.js:663:9)
          at token_error (F:\<omitted>\node_modules\uglify-js\lib\parse.js:671:9)
          at unexpected (F:\<omitted>\node_modules\uglify-js\lib\parse.js:677:9)
          at F:\<omitted>\node_modules\uglify-js\lib\parse.js:761:17
          at F:\<omitted>\node_modules\uglify-js\lib\parse.js:710:24
          at block_ (F:\<omitted>\node_modules\uglify-js\lib\parse.js:984:20)
          at ctor.body (F:\<omitted>\node_modules\uglify-js\lib\parse.js:957:25)
          at function_ (F:\<omitted>\node_modules\uglify-js\lib\parse.js:962:15)

That does seem correct. But are you using uglify from the command line or as a module?

If you're using it as a module (which seems to be the case) you should catch the error yourself.

However, if you're running the uglify command this really shouldn't happen.

I am indeed using it as a module. What makes you think i'm not catching the error myself? I am in fact catching and outputting the exception (how do you think I was able to paste it here?). My issue is that the printed exception doesn't relate to the call I made to uglify (it doesn't tell me what line in my code this call came from), and it doesn't tell me what about the parsing failed. I would expect it to, for example, tell me the file that had the parse failure, and hopefully also the line in that file where it failed (if possible).

I see from your link that the exception has the line and col properties - those should be useful, but its disappointing they don't appear in the exception message. In fact, it doesn't look like there's any error message at all. In any case, where are these things documented - so I can find out what other properties on the exception I should be expecting (so I can appropriately output them)?

Ok, I figured out why JS_parse_error is so bizarre. This is not a properly constructed error. It doesn't even inherit from Error. The stack is created from an error that doesn't have the appropriate message passed in.

Looks like the cause of the incomplete stacktrace is line 16 of tools/node.js: return vm.runInContext(code, UglifyJS, file);. If I understand correctly the runInContext hides the calling context from functions inside the passed code (I guess even in functions called from the resulting object?). Why is it being done this way?

Ugh, I think I can't make JS_Parse_Error an instanceof Error properly because of the use of runInContext. That is incredibly frustrating.

i found a test case for this: when a return is used outside of a function. try minifying this and you'll get a useless stack trace:

if (true) return;

I had this and the problem was in a jade file - I commented it all out and found the problem had gone away, then un-commented block by block until I found the culprit. One bloody comma of course! Good luck.

Having same problem like @jonathanong. Thanks for pointing it out! :cake:

Same here.

:+1:

i personally had this problem when i upgraded the qs module from v4 to v6; upon downgrading to v5 the error disappeared.

+1

> [email protected] build-js /Users/jacopodaeli/Devs/clever/app/packages/pages-admin
> mkdir -p assets/dist/js && browserify -g browserify-css assets/src/js/index.js | uglifyjs > assets/dist/js/index.js

Parse error at -:11345,6
Unexpected token: name (tmp)
Error
    at new JS_Parse_Error (eval at <anonymous> (/Users/jacopodaeli/Devs/clever/app/packages/pages-admin/node_modules/uglify-js/tools/node.js:22:1), <anonymous>:1526:18)
Parse error at -:11345,6
Unexpected token: name (tmp)

Parse error with token named "tmp" on row 11345, column 6 of file "-", a.k.a. stdin, which is the output of browserify in your case.

I've seen better error messages, but this is usually enough to spot the issue in your code. If not, take the same input and run it through a standalone JS parser like acorn or jslint.

I have some problems with uglify one file (other all good)

my log
`[13:05:10] Starting 'scripts'...
[13:05:10] Finished 'scripts' after 12 ms

events.js:141
throw er; // Unhandled 'error' event
^
Error
at new JS_Parse_Error (eval at (/Users/mishapavlenko/Documents/deco.agency/etotdom/wp-content/themes/etotdom/node_modules/uglify-js/tools/node.js:22:1), :1526:18)
at js_error (eval at (/Users/mishapavlenko/Documents/deco.agency/etotdom/wp-content/themes/etotdom/node_modules/uglify-js/tools/node.js:22:1), :1534:11)
at croak (eval at (/Users/mishapavlenko/Documents/deco.agency/etotdom/wp-content/themes/etotdom/node_modules/uglify-js/tools/node.js:22:1), :2026:9)
at token_error (eval at (/Users/mishapavlenko/Documents/deco.agency/etotdom/wp-content/themes/etotdom/node_modules/uglify-js/tools/node.js:22:1), :2034:9)
at unexpected (eval at (/Users/mishapavlenko/Documents/deco.agency/etotdom/wp-content/themes/etotdom/node_modules/uglify-js/tools/node.js:22:1), :2040:9)
at block_ (eval at (/Users/mishapavlenko/Documents/deco.agency/etotdom/wp-content/themes/etotdom/node_modules/uglify-js/tools/node.js:22:1), :2352:28)
at eval (eval at (/Users/mishapavlenko/Documents/deco.agency/etotdom/wp-content/themes/etotdom/node_modules/uglify-js/tools/node.js:22:1), :2120:29)
at eval (eval at (/Users/mishapavlenko/Documents/deco.agency/etotdom/wp-content/themes/etotdom/node_modules/uglify-js/tools/node.js:22:1), :2073:24)
at if_ (eval at (/Users/mishapavlenko/Documents/deco.agency/etotdom/wp-content/themes/etotdom/node_modules/uglify-js/tools/node.js:22:1), :2336:44)
at eval (eval at (/Users/mishapavlenko/Documents/deco.agency/etotdom/wp-content/themes/etotdom/node_modules/uglify-js/tools/node.js:22:1), :2164:24)
`

As written on #1115 it is better to catch these errors manually by catching them yourself or to add an event handler like process.on('uncaughtException', <function>);, even though this method has been discouraged to be used from the node.js community as its main function is to close important connections.

To look for a fix it might be better to ask node/v8 devs to wonder what's going on.

1115 is merged but has no effect on this issue, though a better fix is wanted as this might help more users and not uglifyjs developers alone.

until this is fixed you can find stack trace in ionic by:
npm install uglify-js -g
uglifyjs ./www/build/js/app.bundle.js

two things that are definitely tripping it up are the newer js extensions:

  • reverse singled-quoted / back-ticked strings: this string kills jsuglify
  • promise's coroutines. this example uses npm bluebird: Promise.coroutine(function *() { ... });

... but agree. it would be nice to not resort to deleting swathes of code to see where the problem is. (especially to find out it the code can't be changed to work, as is in the case of function* () {})

@cmroanirgo this issue doesn't track harmony related issues, use the harmony branch or checkout #448

Hi,

I have the same error. When I exec the command through the command line it works, but when I append the command .pipe(uglify()); to the bundle function in the index.js file (inside node_modules/ionic-gulp-browserify-typescript folder) it throws the following exception:

Caught exception: Error at new JS_Parse_Error (eval at <anonymous> (E:\<dir>\node_modules\gulp-uglify\node_modules\uglify-js\tools\node.js:28:1), <anonymous>:1534:18) at js_error (eval at <anonymous> (E:\<dir>\node_modules\gulp-uglify\node_modules\uglify-js\tools\node.js:28:1), <anonymous>:1542:11) at croak (eval at <anonymous> (E:\<dir>\node_modules\gulp-uglify\node_modules\uglify-js\tools\node.js:28:1), <anonymous>:2089:9) at token_error (eval at <anonymous> (E:\<dir>\node_modules\gulp-uglify\node_modules\uglify-js\tools\node.js:28:1), <anonymous>:2097:9) at unexpected (eval at <anonymous> (E:\<dir>\node_modules\gulp-uglify\node_modules\uglify-js\tools\node.js:28:1), <anonymous>:2103:9) at semicolon (eval at <anonymous> (E:\<dir>\node_modules\gulp-uglify\node_modules\uglify-js\tools\node.js:28:1), <anonymous>:2123:56) at simple_statement (eval at <anonymous> (E:\<dir>\node_modules\gulp-uglify\node_modules\uglify-js\tools\node.js:28:1), <anonymous>:2314:73) at eval (eval at <anonymous> (E:\<dir>\node_modules\gulp-uglify\node_modules\uglify-js\tools\node.js:28:1), <anonymous>:2164:47) at eval (eval at <anonymous> (E:\<dir>\node_modules\gulp-uglify\node_modules\uglify-js\tools\node.js:28:1), <anonymous>:2136:24) at block_ (eval at <anonymous> (E:\<dir>\node_modules\gulp-uglify\node_modules\uglify-js\tools\node.js:28:1), <anonymous>:2429:20)

I'm using Ionic ver 2.0.0-beta.37.

@balthier82 That's a question for the gulp people, not uglify.

Uglify offers information where the parse error is when the exception is thrown, but gulp is not printing it.

uglify's lib/parse.js:

function JS_Parse_Error(message, filename, line, col, pos) {
    this.message = message;
    this.filename = filename;
    this.line = line;
    this.col = col;
    this.pos = pos;
    this.stack = new Error().stack;
};

JS_Parse_Error.prototype.toString = function() {
    return this.message + " (line: " + this.line + ", col: " + this.col + ", pos: " + this.pos + ")" + "\n\n" + this.stack;
};

function js_error(message, filename, line, col, pos) {
    throw new JS_Parse_Error(message, filename, line, col, pos);
};

I have the same problem, I'm using angular-fullstack generator, got the same issue when running build:client, I had to debug the gulp script. Maybe you can do something similar?

https://github.com/node-inspector/node-inspector/issues/348

Any word on supporting backtick'd strings and generator functions?

@walshie4 See #448. There is support in the harmony branch in this repository.

@walshie4 Actually, we made enough progress to consider issues with these features as bugs (generators and template strings). As long as you use the harmony branch.

Had the same problem. Why doesn't Uglify print any useful info, when the info itself is there: https://github.com/mishoo/UglifyJS2/blob/master/lib/parse.js#L192

I was able to find out where the error was by printing out those arguments.

To be clear: node doesn't show custom errors except from Error objects. Not sure if this is es spec or not (or might be improved or not).

Workaround is catching the error and handle the error yourself (with try...catch that is).

I just failed a major production release because of that... and I'm still trying to debug it 🈂️
The problem was ES6 syntax. By mistake I just copy pasted some javascript from stackoverflow that contained ES6 syntax. It worked fine on my local comp since I have a recent node, etc. But on my production server it didn't and it was quite hard to debug until I found http://stackoverflow.com/a/38228770/2832282

Harmony support is on the harmony branch, but development is incomplete yet. Currently there is unstable es2016 support, especially lacking modules support. Compressors and manglers are also broke in current harmony. See #448 for that.

Stupid and annoying problem.
I suggested a change:
https://github.com/mishoo/UglifyJS2/compare/master...MariasStory:patch-1
Thanks for the solution suggested in previous comments.

This helped me with what I believe is the same issue:

https://github.com/terinjokes/gulp-uglify/blob/master/docs/why-use-pump/README.md#why-use-pump

Note that not everybody here is using gulp ;-) This error especially reproduces without having to catch an error.

Using uglify through gulp i was getting the error with one file. Removed any use of 'let' keyword in the file and then it worked fine. I may be using an older version, but just commenting in case it helps anyone else out.

There is no harmony support yet on uglify except on the harmony branch

Hi,

Please help, My sanity is on the line

c:\Projects\ObiWanKenobi\client>gulp build
[22:31:28] Using gulpfile c:\Projects\ObiWanKenobi\clientgulpfile.js
[22:31:28] Starting 'build'...
[22:31:28] Starting 'clean-build'...
[22:31:28] Finished 'clean-build' after 45 ms
[22:31:28] Starting 'clean'...
[22:31:28] Finished 'clean' after 19 μs
[22:31:28] Starting 'compile'...
[22:31:33] Finished 'compile' after 4.98 s
[22:31:33] Starting 'copy-assets'...
Copying file app/resources/*/.*

events.js:160
throw er; // Unhandled 'error' event
^
Error
at new JS_Parse_Error (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :1534:18)
at js_error (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :1542:11)
at parse_error (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :1667:9)
at Object.next_token [as input] (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :1946:9)
at next (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :2075:25)
at semicolon (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :2122:30)
at simple_statement (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :2314:73)
at eval (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :2195:24)
at eval (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :2136:24)
at eval (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :2904:23)
at Object.parse (eval at (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:28:1), :2915:7)
at c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:71:33
at Array.forEach (native)
at Object.exports.minify (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\node_modulesuglify-js\tools\node.js:66:15)
at c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\minifier.js:65:22
at trycatch (c:\Projects\ObiWanKenobi\client\node_modulesgulp-uglify\minifier.js:15:12)

If your sanity is on the line, you probably shouldn't be using gulp.

lool, thank you for the confirmation Dr @fresheneesz 🥇

: D

Hi,

New error from the criminally insane.

C:\Projects\Insane\client>gulp build
[15:25:56] Using gulpfile C:\Projects\Insane\clientgulpfile.js
[15:25:56] Starting 'build'...
[15:25:56] Starting 'clean-build'...
[15:25:56] Finished 'clean-build' after 62 ms
[15:25:56] Starting 'clean'...
[15:25:56] Finished 'clean' after 67 μs
[15:25:56] Starting 'compile'...
[15:26:05] Finished 'compile' after 8.51 s
[15:26:05] Starting 'copy-assets'...
Copying file app/resources/*/.*

events.js:160
throw er; // Unhandled 'error' event
^
GulpUglifyError: unable to minify JavaScript
at createError (C:\Projects\Insane\client\node_modulesgulp-uglify\lib\create-error.js:6:14)
at wrapper (C:\Projects\Insane\client\node_modules\lodash_createHybrid.js:87:15)
at trycatch (C:\Projects\Insane\client\node_modulesgulp-uglify\minifier.js:26:12)
at DestroyableTransform.minify [as _transform] (C:\Projects\Insane\client\node_modulesgulp-uglify\minifier.js:76:19)
at DestroyableTransform.Transform._read (C:\Projects\Insane\client\node_modules\through2\node_modules\readable-stream\lib_stream_transform.js:159:10)
at DestroyableTransform.Transform._write (C:\Projects\Insane\client\node_modules\through2\node_modules\readable-stream\lib_stream_transform.js:147:83)
at doWrite (C:\Projects\Insane\client\node_modules\through2\node_modules\readable-stream\lib_stream_writable.js:347:64)
at writeOrBuffer (C:\Projects\Insane\client\node_modules\through2\node_modules\readable-stream\lib_stream_writable.js:336:5)
at DestroyableTransform.Writable.write (C:\Projects\Insane\client\node_modules\through2\node_modules\readable-stream\lib_stream_writable.js:274:11)
at Readable.ondata (_stream_readable.js:555:20)
at emitOne (events.js:96:13)
at Readable.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at Readable.push (_stream_readable.js:134:10)
at C:\Projects\Insane\client\node_modules\fork-stream\index.js:51:13
at classifier (C:\Projects\Insane\client\node_modules\ternary-stream\index.js:20:11)

Hey @alexlamsl, with Rails I am using the uglifier gem

.rvm/gems/ruby-2.3.3/gems/uglifier-3.2.0

And I still get very bad error logs without the source in the CLI. Is that commit still in a non-released branch/gem ?

Hi, all,

I got the same error when I use gulp-uglify.

My debug solution is run uglifyjs directly instead of using gulp then the information from command line point me to the detail syntax error in my source file (a careless usage of let without babel).

Hope it helps.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hacdias picture hacdias  ·  5Comments

Havunen picture Havunen  ·  5Comments

alexlamsl picture alexlamsl  ·  4Comments

alexlamsl picture alexlamsl  ·  5Comments

utdrmac picture utdrmac  ·  4Comments