Registry / web-framework / node-sass-middleware

node-sass-middleware

JSON →
library1.1.0jsnpmunverified

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.

npm install node-sass-middleware
INSTALL
IMPORT
SIG · NODE-SASS-MIDDLEWA
N
node-sass-middleware
web-frameworkjavascriptv1.1.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sassMiddleware
const sassMiddleware = require('node-sass-middleware');
import sassMiddleware from 'node-sass-middleware';
This package is primarily designed for CommonJS (`require`). While Node.js supports ESM, the package's primary usage examples and likely internal structure point to CJS. Direct ESM `import` may not work without a transpiler or specific Node.js configuration.

Demonstrates setting up `node-sass-middleware` with an Express application to compile SASS files on the fly and serve them as static CSS, highlighting the correct middleware order. To run, create a 'sass' directory with a 'main.scss' file (e.g., `body { background-color: lightblue; h1 { color: darkblue; } }`) and an empty 'public' directory in the same location as your script.

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.`); });
Debug
Known issues
breakingThe 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.
fix
For new projects, avoid `node-sass-middleware`. For existing projects, be prepared for potential installation and compatibility issues with newer Node.js versions. Consider migrating to `sass` (Dart Sass) and integrating it via a build process or a custom compilation step for long-term stability.
affects: >=0.1.0
gotchaIncorrect 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.
fix
Always ensure your `app.use(sassMiddleware(...))` call comes *before* `app.use(express.static(...))` in your Express or Connect application setup.
affects: >=0.1.0
gotchaPotential 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`.
fix
Upgrade `node-sass-middleware` to `v0.9.7` or later to benefit from the race condition fix and other improvements for more reliable compilation.
affects: <0.9.7
gotchaBinding 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.
fix
Ensure your Node.js version is compatible with the `node-sass` version used by `node-sass-middleware`. Try running `npm rebuild node-sass` if compilation errors occur. If problems persist, consider containerized environments or migrating to Dart Sass for a more robust solution.
affects: >=0.1.0
Errors
Common errors & fixes
Cannot GET /styles/main.css (or similar 404 for CSS files)
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.
fix
Move `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.
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.
fix
First, 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.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
node-sassrequiredCore dependency for Sass compilation. This library is deprecated and may have compatibility issues with newer Node.js versions.
connectoptionalOptional peer dependency for integration with the Connect framework.
expressoptionalOptional peer dependency for integration with the Express framework.
Agent activity
2 hits · last 30 days
node
2
Resources