GruntJS "Warning: Uglification failed."

I was working with Grunt the other day attempting to update some legacy code to use uglify and I kept getting errors such as:

>> Uglifying source path/to/file.js failed.
Warning: Uglification failed.
Unexpected token punc «(», expected punc «:».
Line 12 in path/to/file.js
 Use --force to continue.

Aborted due to warnings.

When I went to line 12 I saw absolutely nothing wrong. It was a comment block like the following:

/*
An unobtrusive comment goes here
*/

I tried removing the file from the list being included and it happened again. Same error different file. Everything I was reading said it was most likely invalid Javascript doing it, but since I write code perfectly, that could not be the case. Fast forward 10-20 minutes and I found this issue on Github that sounded similar. A comment from Eric Range advised that their code editor had added a line ending of "\n\r" to the file and that caused their build to fail. So I took a look at line endings and sure enough, a handful of the files were using "\n\r". I did a find/replace and changed them all to just a single new line ("\n"). I attempted to uglify it once more and yet again I was shown an error! I was beginning to think uglify and I were not going to be good friends.

Running "uglify:build" (uglify) task
{ message: 'Unexpected token: eof (undefined)',
  filename: 'file.js',
  line: 3032,
  col: 0,
  pos: 129381,
  stack: 'Error\n    at new JS_Parse_Error (/path/to/node/node_modules/grunt-contrib-uglify/node_modules/uglify-js/lib/parse.js:196:18)\n    at js_error (/path/to/nodenode_modules/grunt-contrib-uglify/node_modules/uglify-js/lib/parse.js:204:11)\n    at croak (/path/to/nodenode_modules/grunt-contrib-uglify/node_modules/uglify-js/lib/parse.js:678:41)\n    at token_error (/path/to/nodenode_modules/grunt-contrib-uglify/node_modules/uglify-js/lib/parse.js:682:9)\n    at unexpected (/path/to/nodenode_modules/grunt-contrib-uglify/node_modules/uglify-js/lib/parse.js:688:9)\n    at block_ (/path/to/nodenode_modules/grunt-contrib-uglify/node_modules/uglify-js/lib/parse.js:1000:28)\n    at /path/to/nodenode_modules/grunt-contrib-uglify/node_modules/uglify-js/lib/parse.js:974:25\n    at function_ (/path/to/nodenode_modules/grunt-contrib-uglify/node_modules/uglify-js/lib/parse.js:979:15)\n    at expr_atom (/path/to/nodenode_modules/grunt-contrib-uglify/node_modules/uglify-js/lib/parse.js:1187:24)\n    at maybe_unary (/path/to/nodenode_modules/grunt-contrib-uglify/node_modules/uglify-js/lib/parse.js:1357:19)' }
>> Uglifying source path/to/file.js failed.
Warning: Uglification failed.
Unexpected token: eof (undefined).
Line 3032 in path/to/file.js
 Use --force to continue.

Aborted due to warnings.

I looked at the differences between the files that worked and the ones that failed. The only difference I found was that the failed files used tabs instead of spaces (they were created at a time when there was no convention for tabs vs spaces). So I converted the files that were using tabs to spaces and tried again. Bingo! It worked perfect.

Final Solution

My final solution was to change all files to use unix line endings ("\n") and to convert tabs to spaces.

If you have any feedback for me, I'd love to hear it - corrections, alternative paths, you name it! Send me an email levi@levijackson.xyz