How to uglify JavaScript using UglifyJS 2?

Kazekage Gaara picture Kazekage Gaara · Dec 18, 2013 · Viewed 11.5k times · Source

I tried to uglify a simple javascript file using UglifyJS2.

Here are the contents of the file :

//this is simply a sample var
var sampleVar = "xyz";

//lots of comments
//this is just another comment
//such things should not be present in javascript
//waiting to see result after uglifying

//this is simply a sample function
function sampleFunction()
{
  var sampleLocalVar = "xzx";
  if(true)
  {
    //inserting some sample comments
    alert("in if block");
  }
  else
  {
    //inserting some sample comments
    alert("in else block");
  }
}

Here's the command that I'm using to uglify:

uglifyjs -c -m sample.js sample.min.js

Error that I'm receiving :

Dot
Error parsing arguments in : sample.js

Answer

Qantas 94 Heavy picture Qantas 94 Heavy · Dec 18, 2013

You need to specify the output argument (-o or --output), as the documentation says:

Specify --output (-o) to declare the output file. Otherwise the output goes to STDOUT.

Also, the file to minify (or files to be concatenate and minify) must be specified first, as shown in the usage:

uglifyjs [input files] [options]

What you should be doing is the following:

uglifyjs sample.js -c -m -o sample.min.js

For more information about using UglifyJS2 from the command line, see the documentation.