{"id":12267,"library":"uglify-js","title":"UglifyJS","description":"UglifyJS is a robust JavaScript parser, minifier, compressor, and beautifier toolkit, currently at stable version 3.19.3. It is primarily designed for ECMAScript 5 (ES5) and most modern JavaScript language features, often receiving frequent bug fixes and performance improvements as seen in its recent release cadence. For newer or 'more exotic' ECMAScript features (e.g., beyond ES2015 without transpilation), developers are advised to pre-process their code with a transpiler like Babel before passing it to UglifyJS. Key differentiators for version 3 include a simplified programmatic API and command-line interface compared to its predecessor, UglifyJS 2, and a continued focus on efficient code size reduction through advanced compression and mangling techniques.","status":"active","version":"3.19.3","language":"javascript","source_language":"en","source_url":"https://github.com/mishoo/UglifyJS","tags":["javascript","cli","compress","compressor","ecma","ecmascript","es","es5"],"install":[{"cmd":"npm install uglify-js","lang":"bash","label":"npm"},{"cmd":"yarn add uglify-js","lang":"bash","label":"yarn"},{"cmd":"pnpm add uglify-js","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"The primary programmatic entry point is the `minify` function, often accessed directly from the default export in CommonJS or as a named export in ESM. While `require('uglify-js')` returns an object containing `minify`, direct access is common.","wrong":"const uglify = require('uglify-js');\nconst result = uglify(...);","symbol":"minify","correct":"import { minify } from 'uglify-js';"},{"note":"For CommonJS environments, the entire UglifyJS toolkit is typically imported as a single object named `UglifyJS`, from which `minify` and other utilities are accessed. Native ESM default imports might not work as expected due to the package's primary CJS export.","wrong":"import UglifyJS from 'uglify-js';","symbol":"UglifyJS","correct":"const UglifyJS = require('uglify-js');"},{"note":"UglifyJS exposes its Abstract Syntax Tree (AST) nodes for advanced use cases like custom transformations. These are typically imported as named exports from the main package.","symbol":"AST_Node","correct":"const { AST_Node } = require('uglify-js');"}],"quickstart":{"code":"const UglifyJS = require('uglify-js');\n\nconst code = `\nfunction greet(name) {\n  const message = 'Hello, ' + name + '!';\n  console.log(message);\n  if (name === 'World') {\n    return 'Special greeting';\n  }\n  return 'General greeting';\n}\n\nconst myName = 'World';\ngreet(myName);\n`;\n\nconst options = {\n  compress: {\n    dead_code: true,\n    drop_console: true,\n    reduce_vars: true\n  },\n  mangle: {\n    toplevel: true,\n    properties: false,\n  },\n  output: {\n    comments: 'some',\n    beautify: false,\n  },\n};\n\nconst result = UglifyJS.minify(code, options);\n\nif (result.error) {\n  console.error('Uglification Error:', result.error);\n} else {\n  console.log('Minified code:\\n', result.code);\n  if (result.warnings) {\n    console.warn('Uglification Warnings:', result.warnings);\n  }\n}\n","lang":"javascript","description":"This quickstart demonstrates how to programmatically minify JavaScript code using `uglify-js`, including common compression and mangling options, and handling potential errors or warnings."},"warnings":[{"fix":"Refer to the official UglifyJS 3 documentation for the new API and CLI usage. Specifically, `minify()` is the primary programmatic function.","message":"UglifyJS 3 introduced a completely revised API and CLI that is not backward compatible with UglifyJS 2. Migrating from v2 requires updating import paths and usage patterns.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Integrate a transpilation step (e.g., Babel) into your build process to convert modern JavaScript syntax to ES5 or a compatible target before `uglify-js` processes the files.","message":"UglifyJS is designed primarily for ECMAScript 5 (ES5) and some later features. For code utilizing newer or 'exotic' ECMAScript features (e.g., ES2015+ syntax like optional chaining, nullish coalescing), it is critical to transpile the code down to a compatible syntax (e.g., ES5) using tools like Babel before feeding it to UglifyJS. Failure to do so can lead to `SyntaxError: Unexpected token` errors or incorrect minification.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"If your input is CommonJS, ensure you explicitly disable ES module parsing by setting the appropriate option (e.g., `parse: { module: false }` in the `minify` options, or avoiding `--module` on the CLI) or verify your code's compatibility with ES module parsing.","message":"Since v3.18.0, `uglify-js` processes input as an ES module by default (when using the `--module` CLI option or equivalent API setting). This can alter optimizations for existing CommonJS modules or scripts not explicitly marked as modules, potentially leading to different behavior or errors.","severity":"breaking","affected_versions":">=3.18.0"},{"fix":"Always use the `reserved` option within `mangle.properties` to specify property names that should *never* be mangled. For DOM properties, consider `domprops: false` or carefully use the `builtins` option. Thorough testing after mangling is essential.","message":"Aggressive property mangling (`--mangle-props` or `mangle.properties` option) can break code that relies on specific property names, especially when interacting with external APIs, DOM properties, or libraries that use string-based property access (e.g., `obj['myProp']`).","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Set the `parse.bare_returns: true` option to allow return statements outside of functions. This is particularly useful for CommonJS modules or userscripts wrapped by an external caller.","message":"When minifying code that includes IIFEs (Immediately Invoked Function Expressions) or CommonJS modules, if a `return` statement appears outside of a function, UglifyJS may throw a 'Return outside of function' error.","severity":"gotcha","affected_versions":">=3.0.0"},{"fix":"Upgrade `uglify-js` to version 2.6.0 or greater to mitigate ReDoS vulnerabilities. Regularly updating to the latest stable version is recommended for security patches.","message":"Older versions of UglifyJS (prior to 2.6.0) had known Regular Expression Denial of Service (ReDoS) vulnerabilities in the `parse()` function.","severity":"breaking","affected_versions":"<2.6.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Pre-process your JavaScript files with a transpiler such as Babel to transform modern syntax into ES5 before passing them to `uglify-js`. Ensure your build configuration (e.g., Webpack, Gulp) includes this step.","cause":"Attempting to minify JavaScript code written with modern ES2015+ features (like `const`, `let`, arrow functions, classes) without prior transpilation to a UglifyJS-compatible syntax (primarily ES5).","error":"SyntaxError: Unexpected token: keyword 'const'"},{"fix":"For CommonJS, use `const UglifyJS = require('uglify-js');` and then `UglifyJS.minify(code, options);`. For ESM, use `import { minify } from 'uglify-js';` and then `minify(code, options);`.","cause":"Incorrectly importing or requiring the `uglify-js` package, or attempting to call `minify` directly on the `UglifyJS` object without accessing the function property. In ESM, it could be trying `import UglifyJS from 'uglify-js'` and then `UglifyJS.minify()`.","error":"TypeError: UglifyJS.minify is not a function"},{"fix":"Include `parse: { bare_returns: true }` in your `minify` options to instruct UglifyJS to tolerate `return` statements outside of functions, which is common in certain module patterns.","cause":"The parser encountered a `return` statement in the global scope or within an IIFE/CommonJS module that is not explicitly wrapped as a function that UglifyJS recognizes.","error":"Error: `return` outside of function"},{"fix":"Use the `reserved` option within `mangle.properties` to specify a list of property names that should never be mangled. If mangling DOM properties, set `domprops: false`. Thoroughly test your application after enabling property mangling.","cause":"This often occurs after aggressive property mangling (`--mangle-props` or `mangle.properties`) when UglifyJS renames object properties that are accessed dynamically (e.g., `obj['someProperty']`) or are external interfaces (like DOM properties or third-party library APIs) that should not be renamed.","error":"Cannot read properties of undefined (reading 'someProperty')"}],"ecosystem":"npm"}