Express Uglify Middleware
raw JSON →This package provides an Express middleware designed to dynamically minify JavaScript files using UglifyJS upon request. As of its last known stable release (version 1.0.2), it targets very old Node.js environments (compatible with Node.js >= 0.8.0) and likely older versions of Express and UglifyJS. It enables on-the-fly minification, which was a common pattern in older web applications, but is now largely superseded by build-time asset compilation and minification using tools like Webpack or Vite. The package appears to be abandoned, with no active development or maintenance since its initial release, making it unsuitable for modern JavaScript projects or production use. Its main differentiator was in-request minification, contrasting with modern build-step approaches.
Common errors
error Error: Cannot find module 'uglify-js' ↓
uglify-js is installed locally: npm install uglify-js. error SyntaxError: Unexpected token: keyword 'const' ↓
error TypeError: app.use is not a function ↓
express is correctly installed and imported, and that you are working with an Express Application instance. However, this package is fundamentally incompatible with modern Express versions. error ReferenceError: require is not defined (in browser) ↓
express-uglify-middleware client-side. Warnings
breaking This package is designed for Node.js environments `>= 0.8.0` and is incompatible with modern Node.js versions (e.g., Node.js 12+). Running it on recent Node.js versions will likely result in runtime errors due to API changes and deprecated modules. ↓
breaking The package likely uses an extremely old version of UglifyJS, which does not support modern JavaScript syntax (ES2015+). Attempting to minify ES6+ code will result in `SyntaxError`s or incorrect minification. ↓
breaking This middleware was built for older Express versions (likely 3.x or 4.x). While basic `app.use` might still function, deep incompatibilities with Express 5.x or newer middleware patterns are highly probable, leading to unexpected behavior or errors. ↓
deprecated The pattern of dynamic, on-the-fly minification in a production environment is generally considered an anti-pattern. It adds latency to requests, consumes server resources for each un-cached request, and complicates caching strategies. Modern best practices involve pre-building and minifying assets at deploy time. ↓
gotcha The `force: true` option (as shown in the example) will re-minify files on every request, which is highly inefficient and detrimental to performance in production. While useful for development, it must be set to `false` or removed for production deployments. ↓
Install
npm install express-uglify-middleware yarn add express-uglify-middleware pnpm add express-uglify-middleware Imports
- uglifyMiddleware wrong
import uglifyMiddleware from 'express-uglify-middleware';correctconst uglifyMiddleware = require('express-uglify-middleware');
Quickstart
const express = require('express');
const path = require('path');
const uglifyMiddleware = require('express-uglify-middleware');
const app = express();
const port = 3000;
// Define paths for source JavaScript and public destination
const javascriptSourceDir = path.join(__dirname, 'javascript_src');
const publicJsDestDir = path.join(__dirname, 'public', 'js');
// Create dummy source directory and file for demonstration
require('fs').mkdirSync(javascriptSourceDir, { recursive: true });
require('fs').writeFileSync(path.join(javascriptSourceDir, 'app.js'), 'function hello(name) { console.log("Hello, " + name); } hello("World");');
// Configure and use the uglifyMiddleware
app.use(uglifyMiddleware({
src: javascriptSourceDir,
dest: publicJsDestDir,
prefix: '/js',
compressFilter: /\.js$/,
compress: true,
force: true, // Forces re-compression on every request (for testing)
debug: true
}));
// Serve static files from the 'public' directory (e.g., the minified JS)
app.use('/public', express.static(path.join(__dirname, 'public')));
// Basic route to demonstrate
app.get('/', (req, res) => {
res.send('<h1>Express Uglify Middleware Demo</h1><p>Visit <a href="/js/app.js">/js/app.js</a> to see minified output.</p><p>Check the console for debug messages if `debug: true`.</p>');
});
app.listen(port, () => {
console.log(`Server listening at http://localhost:${port}`);
console.log(`Source JS: ${javascriptSourceDir}`);
console.log(`Minified JS (expected): ${path.join(publicJsDestDir, 'app.js')}`);
console.log('You might need to clear browser cache for changes to appear if `force: false`.');
});