note: You can support this project on patreon: . Check out PATRONS.md for our first-tier patrons.
Terser recommends you use RollupJS to bundle your modules, as that produces smaller code overall.
Beautification has been undocumented and is being removed from terser, we recommend you use prettier.
Find the changelog in CHANGELOG.md
Why choose terser?
uglify-es is no longer maintained and
uglify-js does not support ES6+.
terser is a fork of
uglify-es that mostly retains API and CLI compatibility
First make sure you have installed the latest version of node.js (You may need to restart your computer after this step).
From NPM for use as a command line app:
npm install terser -g
From NPM for programmatic use:
npm install terser
Command line usage
terser [input files] [options]
Terser can take multiple input files. It's recommended that you pass the input files first, then pass the options. Terser will parse input files in sequence and apply any compression options. The files are parsed in the same global scope, that is, a reference from a file to some variable/function declared in another file will be matched properly.
If no input file is specified, Terser will read from STDIN.
If you wish to pass your options before the input files, separate the two with a double dash to prevent input files being used as option arguments:
terser --compress --mangle -- input.js
Command line options
-o) to declare the output file. Otherwise the output
goes to STDOUT.
CLI source map options
Terser can generate a source map file, which is highly useful for
--source-map --output output.js (source map will be written out to
--source-map "filename='<NAME>'"to specify the name of the source map.
--source-map "root='<URL>'"to pass the URL where the original files can be found.
--source-map "url='<URL>'"to specify the URL where the source map can be found. Otherwise Terser assumes HTTP
X-SourceMapis being used and will omit the
terser js/file1.js js/file2.js \ -o foo.min.js -c -m \ --source-map "root='http://foo.com/src',url='foo.min.js.map'"
The above will compress and mangle
file2.js, will drop the
foo.min.js and the source map in
foo.min.js.map. The source
mapping will refer to
http://foo.com/src/js/file2.js (in fact it will list
as the source map root, and the original files as
Composed source map
When you're compressing JS code that was output by a compiler such as CoffeeScript, mapping to the JS code won't be too helpful. Instead, you'd like to map back to the original code (i.e. CoffeeScript). Terser has an option to take an input source map. Assuming you have a mapping from CoffeeScript → compiled JS, Terser can generate a map from CoffeeScript → compressed JS by mapping every token in the compiled JS to its original location.
To use this feature pass
--source-map "content=inline" if the source map is included inline with
CLI compress options
You need to pass
-c) to enable the compressor. Optionally
you can pass a comma-separated list of compress options.
Options are in the form
foo=bar, or just
foo (the latter implies
a boolean option that you want to set
true; it's effectively a
terser file.js -c toplevel,sequences=false
CLI mangle options
To enable the mangler you need to pass
-m). The following
(comma-separated) options are supported:
false) -- mangle names declared in the top level scope.
false) -- mangle names visible in scopes where
When mangling is enabled but you want to prevent certain names from being
mangled, you can declare those names with
--mangle reserved — pass a
comma-separated list of names. For example:
terser ... -m reserved=['