Mollify Node.js Minify Middleware
raw JSON →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.
Common errors
error TypeError: mollify is not a function ↓
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 ↓
__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) ↓
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. Warnings
breaking Version 6.0.0 dropped support for Node.js versions older than 16. Ensure your environment meets this requirement before upgrading. ↓
breaking Version 5.0.0 converted the package to pure ESM. CommonJS `require()` statements will no longer work, and you must use `import` syntax. ↓
breaking Version 4.0.0 dropped support for Node.js versions older than 14. ↓
breaking Version 3.0.0 dropped support for Node.js versions older than 12. ↓
Install
npm install mollify yarn add mollify pnpm add mollify Imports
- mollify wrong
const mollify = require('mollify');correctimport mollify from 'mollify'; - fileURLToPath wrong
const { fileURLToPath } = require('url');correctimport { fileURLToPath } from 'url'; - dirname wrong
const { dirname } = require('path');correctimport { dirname } from 'path';
Quickstart
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}`);
});