Node Sass Middleware for Connect/Express
node-sass-middleware is a Connect/Express middleware designed to automatically recompile `.scss` or `.sass` files on demand for web servers. It leverages the `node-sass` library (which in turn uses LibSass) to provide on-the-fly compilation, caching, and debugging capabilities directly within a Node.js web application stack. The package's current stable version is 1.1.0, with its latest patch release (v1.1.0) occurring in 2024. While the package itself sees intermittent updates, its core dependency, `node-sass`, is officially deprecated and no longer actively maintained. This means `node-sass-middleware` is primarily suited for maintaining existing projects rather than new development, where modern Dart Sass solutions are generally preferred. It integrates seamlessly into existing Connect or Express applications, offering a convenient bridge between Sass source files and served CSS.
Common errors
-
Cannot GET /styles/main.css (or similar 404 for CSS files)
cause The `node-sass-middleware` is placed after `express.static` (or `connect.static`). The static middleware intercepts the request for the CSS file before `node-sass-middleware` can compile and serve it, leading to a 404.fixMove `app.use(sassMiddleware(...))` to occur *before* `app.use(express.static(...))` in your application's middleware chain. -
Node Sass does not yet support your current environment: OS X 64-bit with Node.js X. Run `npm rebuild node-sass` to try to fix this.
cause The native bindings for `node-sass` are not compatible with your current Node.js version or operating system architecture. This is a common issue with `node-sass` due to its reliance on platform-specific compiled binaries.fixFirst, try running `npm rebuild node-sass`. If that doesn't work, ensure your Node.js version falls within the range explicitly supported by the `node-sass` version `node-sass-middleware` depends on. As a long-term solution, consider migrating to `sass` (Dart Sass) which does not have native binding dependencies.
Warnings
- breaking The underlying `node-sass` library, which `node-sass-middleware` critically depends on, is officially deprecated. `node-sass` has known issues with newer Node.js versions and platform-specific native bindings, and it is no longer actively developed. For new projects, it is strongly recommended to use `sass` (Dart Sass) with a build tool or a different middleware solution.
- gotcha Incorrect middleware order: `node-sass-middleware` *must* be placed before any static file serving middleware (like `express.static`). If `express.static` runs first, it will attempt to serve a non-existent CSS file directly, resulting in 404 errors and preventing `node-sass-middleware` from compiling and serving the SASS source.
- gotcha Potential race conditions or unexpected compilation issues might occur for users on older versions (pre-v0.9.7). A critical bugfix addressing a race condition issue was implemented in `v0.9.7`.
- gotcha Binding compatibility issues for `node-sass`: Historically, `node-sass` has faced challenges with specific Node.js versions or operating systems regarding its native bindings. While `node-sass-middleware` updates its `node-sass` dependency, users might still encounter these issues, leading to compilation failures.
Install
-
npm install node-sass-middleware -
yarn add node-sass-middleware -
pnpm add node-sass-middleware
Imports
- sassMiddleware
import sassMiddleware from 'node-sass-middleware';
const sassMiddleware = require('node-sass-middleware');
Quickstart
const express = require('express');
const sassMiddleware = require('node-sass-middleware');
const path = require('path');
const app = express();
// IMPORTANT: Place sassMiddleware *before* express.static
app.use(sassMiddleware({
src: path.join(__dirname, 'sass'), // Source directory for .scss/.sass files
dest: path.join(__dirname, 'public'), // Destination directory for .css files
debug: true, // Output debugging info to console
outputStyle: 'compressed', // Minify CSS output
prefix: '/styles' // CSS will be served from /styles/your_file.css
}));
// Serve static files from the 'public' directory, including compiled CSS
app.use('/styles', express.static(path.join(__dirname, 'public')));
// Example route for demonstration
app.get('/', (req, res) => {
res.send('<!DOCTYPE html><html><head><link rel="stylesheet" href="/styles/main.css"></head><body><h1>Hello from node-sass-middleware!</h1></body></html>');
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server listening on port ${PORT}`);
console.log(`Open http://localhost:${PORT} in your browser.`);
console.log(`Ensure you have a 'sass' directory with 'sass/main.scss' (e.g., body { h1 { color: darkblue; } }) and an empty 'public' directory.`);
});