UglifyJS
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.
Common errors
-
SyntaxError: Unexpected token: keyword 'const'
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).fixPre-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. -
TypeError: UglifyJS.minify is not a function
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()`.fixFor 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);`. -
Error: `return` outside of function
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.fixInclude `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. -
Cannot read properties of undefined (reading 'someProperty')
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.fixUse 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.
Warnings
- breaking 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.
- gotcha 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.
- breaking 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.
- gotcha 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']`).
- gotcha 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.
- breaking Older versions of UglifyJS (prior to 2.6.0) had known Regular Expression Denial of Service (ReDoS) vulnerabilities in the `parse()` function.
Install
-
npm install uglify-js -
yarn add uglify-js -
pnpm add uglify-js
Imports
- minify
const uglify = require('uglify-js'); const result = uglify(...);import { minify } from 'uglify-js'; - UglifyJS
import UglifyJS from 'uglify-js';
const UglifyJS = require('uglify-js'); - AST_Node
const { AST_Node } = require('uglify-js');
Quickstart
const UglifyJS = require('uglify-js');
const code = `
function greet(name) {
const message = 'Hello, ' + name + '!';
console.log(message);
if (name === 'World') {
return 'Special greeting';
}
return 'General greeting';
}
const myName = 'World';
greet(myName);
`;
const options = {
compress: {
dead_code: true,
drop_console: true,
reduce_vars: true
},
mangle: {
toplevel: true,
properties: false,
},
output: {
comments: 'some',
beautify: false,
},
};
const result = UglifyJS.minify(code, options);
if (result.error) {
console.error('Uglification Error:', result.error);
} else {
console.log('Minified code:\n', result.code);
if (result.warnings) {
console.warn('Uglification Warnings:', result.warnings);
}
}