API Reference

Terser

JavaScript minifier and mangler with a programmatic API for compressing, mangle-renaming, and rewriting bundles.

Official Documentation ↗
CompatibilityThis documentation covers version v5. Check the official docs for the latest API surface.

Installation

npmnpm install --save-dev terser
pnpmpnpm add -D terser

Methods

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.

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.