less-middleware is an HTTP middleware specifically designed to process LESS CSS files for web servers built with Connect.js or Express.js. It compiles .less files into .css on-the-fly when requested, or upon server restart with caching, and serves the resulting CSS. The current stable version, 3.1.0, is compatible with Less.js 3.9. While not on a strict schedule, releases typically follow major Less.js updates to maintain compatibility and leverage new features. Its key differentiators include robust caching mechanisms (including `cacheFile` for cross-restart caching), extensive preprocessing and postprocessing hooks (`preprocess.less`, `postprocess.css`), and fine-grained control over rendering options, making it suitable for both development and production environments by reducing disk I/O.
npm install less-middlewareVerified import paths — ran on the pinned version, not inferred.
This Express.js quickstart demonstrates how to integrate `less-middleware` to compile LESS files on the fly. It sets up the middleware to serve static files, enabling LESS compilation in a 'public' directory with different caching strategies for development and production environments, and basic Less rendering options like compression.
Review the Less.js 3.x changelog for any changes that might affect your .less files. Ensure your Less syntax is compatible with Less 3.
Replace any usage of `options.parser` with `options.render` for passing rendering-specific options. Consult the less-middleware 2.x README for the new options structure.
Always explicitly define `dest` to a desired output directory, especially if source LESS files are not in the same directory where compiled CSS should reside. Ensure `pathRoot` is correctly set if your source and destination directories share a common, non-root parent.
For development, set `force: true` and `debug: true`. For production, set `once: true` and consider using `cacheFile` to persist import dependency information across server restarts. Ensure `process.env.NODE_ENV` is correctly set for conditional logic.
Verify the `source` option points to the correct directory containing your .less files. Check file paths, capitalization, and ensure read permissions. Use `debug: true` in options for more verbose logging about file resolution.
Ensure you have correctly initialized your Express or Connect application, e.g., `const express = require('express'); const app = express();` before calling `app.use(lessMiddleware(...));`.Install the `less` package: `npm install less --save`. Ensure its version is compatible with your `less-middleware` version.