How to Use Terser via CLI or Node.js API
Updated
Terser is a JavaScript mangler and compressor toolkit that shrinks variable names, removes whitespace and comments, and drops unused code. You can run it two ways: through the Command line for one-off files and build scripts, or through the Node.JS API when you need to minify code programmatically inside a tool or pipeline. Both paths expose the same core options, so the choice comes down to whether you want a shell command or a JavaScript function call.
Using the Terser CLI
The CLI is the fastest route for minifying a file or a directory of files.
Basic usage
terser input.js -o output.js
- Input: the source file you pass as the first argument.
- Action: Terser parses the file, applies mangling and compression, and writes the result.
- Expected result:
output.jscontains the minified code. If you omit-o, the result is printed to standard output.
Common options
| Option | What it does |
|---|---|
-c / --compress |
Enables the compressor (drops unused code, simplifies expressions). |
-m / --mangle |
Shortens variable and function names. |
-o / --output |
Writes the result to a file instead of stdout. |
--source-map |
Emits a source map alongside the output. |
A typical production command combines them:
terser input.js -c -m -o output.min.js --source-map "url='output.min.js.map'"
Verifying the result
After running, open output.min.js and confirm that whitespace and comments are gone and identifiers are shortened. If the file still looks unminified, the compressor or mangler flag was likely omitted.
Common snags
- Broken code after mangling: mangling renames identifiers, so code that relies on
Function.prototype.nameor string-based lookups can break. Disable mangling for those cases or exclude specific names. - No output file: if you forget
-o, the minified code goes to stdout and may scroll past in the terminal.
Using the Terser Node.js API
The API is the right choice when minification is part of a larger JavaScript program — a custom build step, a bundler plugin, or a server-side transform.
Basic usage
const { minify } = require("terser");
const code = `function add(first, second) { return first + second; }`;
minify(code, {
compress: true,
mangle: true,
}).then((result) => {
console.log(result.code);
});
- Input: the source string as the first argument, and an options object as the second.
- Action:
minifyreturns a promise that resolves to an object. - Expected result:
result.codeholds the minified string. If minification fails,result.erroris set instead.
Configuring compress and mangle
Both compress and mangle accept either a boolean or an options object for finer control:
minify(code, {
compress: {
drop_console: true,
passes: 2,
},
mangle: {
toplevel: true,
},
});
compresscontrols code-level optimizations such as removing unused statements.manglecontrols identifier renaming;toplevel: trueextends renaming to top-level names.
Handling the result
Always check for errors before using the output:
const result = await minify(code, { compress: true, mangle: true });
if (result.error) {
throw result.error;
}
console.log(result.code);
Where to Find Full Documentation
Terser publishes dedicated references for both interfaces:
- CLI Docs — every command-line flag and its behavior.
- API Reference — the full options object for
minifyand related functions. - Options — the shared list of compress and mangle settings.
- REPL — a browser playground for testing options against sample code before wiring them into a build.
The project also maintains a GitHub repository and a Stack Overflow community tag for questions. Because Terser is described as an industry-standard minifier, its options surface is broad; the REPL is the quickest way to confirm what a given flag does to your code before committing it to a build script.