{"id":12131,"library":"terser","title":"Terser","description":"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.","status":"active","version":"5.46.1","language":"javascript","source_language":"en","source_url":"https://github.com/terser/terser","tags":["javascript","uglify","terser","uglify-es","uglify-js","minify","minifier","ecmascript","typescript"],"install":[{"cmd":"npm install terser","lang":"bash","label":"npm"},{"cmd":"yarn add terser","lang":"bash","label":"yarn"},{"cmd":"pnpm add terser","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"CommonJS users typically destructure `minify` from the `require` call. ES Modules use named import for this function.","wrong":"const minify = require('terser').minify;","symbol":"minify","correct":"import { minify } from 'terser';"},{"note":"For TypeScript, use `import type` when importing only type definitions to ensure they are stripped during compilation and do not affect runtime.","wrong":"import { MinifyOptions } from 'terser';","symbol":"MinifyOptions","correct":"import type { MinifyOptions } from 'terser';"},{"note":"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`.","wrong":"import Terser from 'terser';","symbol":"All exports (namespace import)","correct":"import * as Terser from 'terser';"}],"quickstart":{"code":"import { minify } from 'terser';\n\nconst code = `\nclass Greeter {\n  constructor(name) {\n    this.name = name;\n  }\n\n  greet() {\n    return 'Hello, ' + this.name + '!';\n  }\n}\n\nconst greeter = new Greeter('World');\nconsole.log(greeter.greet());\n`;\n\nasync function runMinification() {\n  try {\n    const result = await minify(code, {\n      compress: {\n        dead_code: true,\n        drop_console: true,\n        ecma: 2020\n      },\n      mangle: {\n        toplevel: true\n      }\n    });\n    if (result.error) {\n      console.error('Minification Error:', result.error);\n    } else {\n      console.log('Minified Code:\\n', result.code);\n      // Expected output will be a highly compressed version of the input JS\n      // e.g., 'class Greeter{constructor(e){this.name=e}greet(){return\"Hello, \"+this.name+\"!\"}}const greeter=new Greeter(\"World\");console.log(greeter.greet());'\n    }\n  } catch (e) {\n    console.error('Unexpected Error:', e);\n  }\n}\n\nrunMinification();","lang":"typescript","description":"Demonstrates how to programmatically minify a JavaScript string using `terser`, including compression and mangling options for ES2020 syntax."},"warnings":[{"fix":"Replace `uglify-es` or `uglify-js` with `terser` in `package.json` and update build scripts accordingly.","message":"`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.","severity":"breaking","affected_versions":"<=4.x"},{"fix":"Remove any `beautify` related options from `terser` configuration. Integrate `prettier` into your development workflow for code formatting.","message":"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.","severity":"deprecated","affected_versions":">=5.x"},{"fix":"Command line usage: `terser --compress --mangle -- input.js` instead of `terser --compress --mangle input.js`.","message":"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.","severity":"gotcha","affected_versions":">=1.0"},{"fix":"Upgrade your Node.js environment to version 10 or newer. Check your `package.json` `engines` field for compatibility.","message":"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.","severity":"breaking","affected_versions":"<=9.x (Node.js)"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Ensure `terser` is installed and configured as your JavaScript minifier in your build pipeline, and that `uglify-js` is not accidentally being used.","cause":"Attempting to minify modern JavaScript (ES6+) with an older minifier like `uglify-js` instead of `terser`.","error":"SyntaxError: Unexpected token '...' (or similar for ES6+ features)"},{"fix":"For ESM: `import { minify } from 'terser';`. For CommonJS: `const { minify } = require('terser');`","cause":"Incorrectly importing or accessing the `minify` function, often due to a wrong CommonJS `require` pattern or an invalid default import in ESM.","error":"TypeError: terser.minify is not a function"},{"fix":"Remove any `beautify` options from your `terser` configuration. Use a dedicated code formatter like Prettier instead for beautification.","cause":"Using the deprecated `beautify` option in `terser` which has been removed in favor of external formatters.","error":"Error: 'beautify' option is not recognized or has no effect"}],"ecosystem":"npm"}