Terser
JavaScript minifier and mangler with a programmatic API for compressing, mangle-renaming, and rewriting bundles.
Official Documentation ↗v5. Check the official docs for the latest API surface.Installation
npm install --save-dev terserpnpm add -D terserMethods
No methods match this filter.
Overview
Terser is a JavaScript minifier and mangler that compresses bundles and optionally renames properties, with promise and sync APIs. This reference lists the most frequently used APIs with accurate signatures and runnable snippets.
Core APIs
minify— Asynchronously compresses source and resolves with the minified code and source map.minify_sync— Synchronous minification for scripts that cannot await.mangle options— Controls identifier renaming, including optional property name mangling guarded by regex filters.
Example
const { minify } = require('terser');
const out = await minify('function add(a, b) { return a + b; }', {
compress: true,
mangle: true,
});
console.log(out.code);
Notes
Property mangling is powerful but dangerous for reflection and dynamic access; scope it with a regex and reserved lists. Supply source map content for correct mappings.
Verification
Minify a function, confirm it still returns the same value when executed, and verify the emitted source map resolves to the original file.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/terser/terser@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:52:56.798Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- By default, it will mangle all properties in the input code with the exception of built in DOM properties and properties in core JavaScript classes, which is what will break your code if you don’t: 1. (https://cdn.jsdelivr.net/gh/terser/terser@latest/README.md)
- Specify “strict” to treat foo.bar as side-effect-free only when foo is certain to not throw, i.e. (https://cdn.jsdelivr.net/gh/terser/terser@latest/README.md)
FAQ
- Why did minification change my code behavior?
- Mangled property names break code that accesses those names dynamically; reserve such properties or disable property mangling.
- How do I keep a variable name unmangled?
- List it in mangle.reserved, or annotate the source with the /*@__PURE__*/ style comments that Terser honors.
- How do I emit a source map?
- Set sourceMap: true and pass content in the source map options so Terser can map back to the original sources.
Report a problem with this API docOpens a pre-filled issue on GitHub.