Express Uglify Middleware

raw JSON →
1.0.2 verified Thu Apr 23 auth: no javascript abandoned

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.

error Error: Cannot find module 'uglify-js'
cause The `uglify-js` package, a peer dependency, was not installed or is inaccessible.
fix
Ensure uglify-js is installed locally: npm install uglify-js.
error SyntaxError: Unexpected token: keyword 'const'
cause The version of UglifyJS used by this middleware does not support ES6 (ECMAScript 2015) or newer syntax.
fix
This package cannot minify modern JavaScript. You must either transpile your code to ES5 before passing it to this middleware or, preferably, migrate to a modern build pipeline with an up-to-date minifier like Terser.
error TypeError: app.use is not a function
cause This error typically indicates an issue with how Express is being initialized or an incompatibility with an extremely old (pre-2.x) or corrupted Express installation.
fix
Verify that 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)
cause This package is a Node.js server-side middleware and cannot be run directly in a browser environment.
fix
This middleware is for server-side processing with Express. It generates static assets that are then served to the browser. Do not attempt to bundle or run express-uglify-middleware client-side.
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.
fix Do not use this package in modern Node.js environments. For current projects, utilize build tools like Webpack or Vite with Terser for JavaScript minification.
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.
fix Avoid using this middleware for modern JavaScript. Modern projects should use build tools with up-to-date minifiers like Terser that support the latest ECMAScript features.
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.
fix This package is not compatible with modern Express applications. Consider a different approach for asset management that integrates with current Express practices.
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.
fix Adopt a build-time asset compilation workflow (e.g., Webpack, Rollup, Vite) to minify JavaScript before deployment. Serve pre-minified assets as static files, potentially via a CDN, for optimal performance and scalability.
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.
fix Ensure `force: false` (or omit it for the default `false`) in production environments to enable caching of minified files and avoid redundant processing.
npm install express-uglify-middleware
yarn add express-uglify-middleware
pnpm add express-uglify-middleware

This quickstart sets up a basic Express application that uses `express-uglify-middleware` to dynamically minify a source JavaScript file into a public directory, serving it via a '/js' prefix. It demonstrates the configuration options and a simple Express server setup.

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`.');
});