Mollify Node.js Minify Middleware

raw JSON →
6.0.0 verified Thu Apr 23 auth: no javascript

Mollify is a Node.js middleware that integrates the `minify` package, providing on-the-fly minification of static assets like JavaScript, CSS, and HTML files. It is primarily designed for use with web frameworks such as Express, streamlining the process of serving optimized content. The current stable version is 6.0.0, released after several iterative updates that include dropping support for older Node.js versions (now requiring Node.js >=16) and a significant transition to ESM in version 5.0.0. The package has a moderate release cadence, often driven by updates to its core `minify` dependency or environmental changes. Its key differentiator lies in its straightforward integration as an Express middleware, offering a simple solution for asset optimization without requiring complex build pipelines, making it suitable for rapid development and certain production environments.

error TypeError: mollify is not a function
cause Attempting to use `require('mollify')` in an ESM context, or calling `mollify` directly when it might be a default export that needs `mollify.default` in some mixed setups, or `require` in an ESM module.
fix
For mollify v5.0.0+, ensure you are using import mollify from 'mollify';. If in an older CommonJS context, ensure require('mollify') is used correctly as a function.
error ReferenceError: __dirname is not defined
cause Using `__dirname` or `__filename` directly in an ESM module without defining them, as they are CommonJS-specific globals.
fix
Use the provided ESM boilerplate to define __filename and __dirname from import.meta.url: import {fileURLToPath} from 'url'; import {dirname} from 'path'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename);
error Files are not being minified (but mollify is enabled)
cause The `dir` option passed to mollify does not correctly point to the directory containing the static files, or `is: false` is explicitly set.
fix
Verify that the dir option in mollify({ dir: ... }) accurately reflects the root directory where express.static is serving files from. Ensure is: true or omit the is property as true is the default.
breaking Version 6.0.0 dropped support for Node.js versions older than 16. Ensure your environment meets this requirement before upgrading.
fix Upgrade Node.js to version 16 or higher, or pin 'mollify' to a version compatible with your current Node.js (e.g., `<6.0.0` for Node.js <16).
breaking Version 5.0.0 converted the package to pure ESM. CommonJS `require()` statements will no longer work, and you must use `import` syntax.
fix Migrate your project to ESM if not already, or use dynamic `import()` if you need to load mollify from a CommonJS context (though this is not recommended). Update all `require('mollify')` to `import mollify from 'mollify'`.
breaking Version 4.0.0 dropped support for Node.js versions older than 14.
fix Upgrade Node.js to version 14 or higher.
breaking Version 3.0.0 dropped support for Node.js versions older than 12.
fix Upgrade Node.js to version 12 or higher.
npm install mollify
yarn add mollify
pnpm add mollify

This quickstart demonstrates setting up an Express server that uses mollify middleware to serve minified static content from the current directory.

import {fileURLToPath} from 'url';
import {dirname} from 'path';
import http from 'http';
import mollify from 'mollify';
import express from 'express';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

const app = express();
const server = http.createServer(app);

const port = process.env.PORT ?? 1337;
const ip = process.env.IP ?? '0.0.0.0';

app.use(mollify({
    dir: __dirname,
    is: true, // default
}));

app.use(express.static(__dirname));

server.listen(port, ip, () => {
  console.log(`Server listening on http://${ip}:${port}`);
  console.log(`Serving static files from: ${__dirname}`);
});