Terser
Terser is a powerful JavaScript parser, mangler, and compressor toolkit designed for modern ECMAScript (ES6+). It currently stands at version 5.46.1 and is under active development, serving as the maintained successor to the now-abandoned `uglify-es` project and `uglify-js` (which lacks ES6+ support). Terser largely retains API and CLI compatibility with its predecessors, making it a familiar choice for developers. Key differentiators include its robust support for modern JavaScript syntax and ongoing maintenance, addressing the limitations of older minifiers. While `terser` previously offered beautification features, these are being removed in favor of dedicated tools like Prettier. The project recommends using it in conjunction with bundlers like RollupJS for optimal output size.
Common errors
-
SyntaxError: Unexpected token '...' (or similar for ES6+ features)
cause Attempting to minify modern JavaScript (ES6+) with an older minifier like `uglify-js` instead of `terser`.fixEnsure `terser` is installed and configured as your JavaScript minifier in your build pipeline, and that `uglify-js` is not accidentally being used. -
TypeError: terser.minify is not a function
cause Incorrectly importing or accessing the `minify` function, often due to a wrong CommonJS `require` pattern or an invalid default import in ESM.fixFor ESM: `import { minify } from 'terser';`. For CommonJS: `const { minify } = require('terser');` -
Error: 'beautify' option is not recognized or has no effect
cause Using the deprecated `beautify` option in `terser` which has been removed in favor of external formatters.fixRemove any `beautify` options from your `terser` configuration. Use a dedicated code formatter like Prettier instead for beautification.
Warnings
- breaking `uglify-es` is no longer maintained and `uglify-js` does not support ES6+ syntax. Projects using older minifiers for modern JavaScript should migrate to `terser` to avoid build failures or incorrect output.
- deprecated The `beautification` features in `terser` are being removed. Using beautify options may lead to unexpected behavior or future breakage. For code formatting, use a dedicated tool like Prettier.
- gotcha When using the `terser` CLI, if you pass options *before* input files, you must separate them with a double dash (`--`) to prevent input files from being interpreted as option arguments.
- breaking Terser requires Node.js version 10 or higher. Running with older Node.js versions will result in errors or installation failures due to engine requirements.
Install
-
npm install terser -
yarn add terser -
pnpm add terser
Imports
- minify
const minify = require('terser').minify;import { minify } from 'terser'; - MinifyOptions
import { MinifyOptions } from 'terser';import type { MinifyOptions } from 'terser'; - All exports (namespace import)
import Terser from 'terser';
import * as Terser from 'terser';
Quickstart
import { minify } from 'terser';
const code = `
class Greeter {
constructor(name) {
this.name = name;
}
greet() {
return 'Hello, ' + this.name + '!';
}
}
const greeter = new Greeter('World');
console.log(greeter.greet());
`;
async function runMinification() {
try {
const result = await minify(code, {
compress: {
dead_code: true,
drop_console: true,
ecma: 2020
},
mangle: {
toplevel: true
}
});
if (result.error) {
console.error('Minification Error:', result.error);
} else {
console.log('Minified Code:\n', result.code);
// Expected output will be a highly compressed version of the input JS
// e.g., 'class Greeter{constructor(e){this.name=e}greet(){return"Hello, "+this.name+"!"}}const greeter=new Greeter("World");console.log(greeter.greet());'
}
} catch (e) {
console.error('Unexpected Error:', e);
}
}
runMinification();