Registry / serialization / terser

terser

JSON →
library5.46.1jsnpmunverified

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.

npm install terser
INSTALL
IMPORT
SIG · TERSER
T
terser
serializationjavascriptv5.46.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

minify
import { minify } from 'terser';
const minify = require('terser').minify;
CommonJS users typically destructure `minify` from the `require` call. ES Modules use named import for this function.
MinifyOptions
import type { MinifyOptions } from 'terser';
import { MinifyOptions } from 'terser';
For TypeScript, use `import type` when importing only type definitions to ensure they are stripped during compilation and do not affect runtime.
All exports (namespace import)
import * as Terser from 'terser';
import Terser from 'terser';
Terser does not provide a default export. If you need to access multiple exports or prefer a namespace import, use `import * as Terser from 'terser';` and then access functions like `Terser.minify`.

Demonstrates how to programmatically minify a JavaScript string using `terser`, including compression and mangling options for ES2020 syntax.

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();
terser --version
Debug
Known issues
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.
fix
Replace `uglify-es` or `uglify-js` with `terser` in `package.json` and update build scripts accordingly.
affects: <=4.x
deprecatedThe `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.
fix
Remove any `beautify` related options from `terser` configuration. Integrate `prettier` into your development workflow for code formatting.
affects: >=5.x
gotchaWhen 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.
fix
Command line usage: `terser --compress --mangle -- input.js` instead of `terser --compress --mangle input.js`.
affects: >=1.0
breakingTerser requires Node.js version 10 or higher. Running with older Node.js versions will result in errors or installation failures due to engine requirements.
fix
Upgrade your Node.js environment to version 10 or newer. Check your `package.json` `engines` field for compatibility.
affects: <=9.x (Node.js)
Errors
Common errors & fixes
SyntaxError: Unexpected token '...' (or similar for ES6+ features)
Attempting to minify modern JavaScript (ES6+) with an older minifier like `uglify-js` instead of `terser`.
fix
Ensure `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
Incorrectly importing or accessing the `minify` function, often due to a wrong CommonJS `require` pattern or an invalid default import in ESM.
fix
For ESM: `import { minify } from 'terser';`. For CommonJS: `const { minify } = require('terser');`
Error: 'beautify' option is not recognized or has no effect
Using the deprecated `beautify` option in `terser` which has been removed in favor of external formatters.
fix
Remove any `beautify` options from your `terser` configuration. Use a dedicated code formatter like Prettier instead for beautification.
Upgrade
Version history
5.46.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
9 hits · last 30 days
node
8
OpenAI (training)
1
Resources
terser — npm install terser · libregistry