Sass (Pure JavaScript Implementation)
The `sass` npm package provides a pure JavaScript implementation of the Sass preprocessor, enabling developers to compile SCSS and Sass files into CSS. It is currently at version 1.99.0 and receives frequent, iterative updates, with new features and bug fixes released regularly as seen in its changelog. This package is a compilation of Dart Sass to JavaScript, making it highly portable with no native dependencies, unlike `node-sass` which relies on LibSass (C++). It offers both a command-line interface and a Node.js API, featuring modern asynchronous and synchronous `compile` functions for transforming Sass code. While it maintains a legacy API compatible with `node-sass` (`render`, `renderSync`), this API is deprecated and slated for removal in Dart Sass 2.0.0, distinguishing its usage patterns from older Sass integrations. Its key differentiators include platform independence, official support from the Sass team, and predictable evolution, providing a robust and evolving solution for CSS preprocessing in JavaScript environments.
Common errors
-
Error: `render()` and `renderSync()` are no longer supported. Use `compile()` or `compileAsync()` instead.
cause Attempting to use the deprecated legacy `render` or `renderSync` functions from the `sass` package.fixUpdate your code to use the modern `compile()` or `compileAsync()` functions. The API signatures and return types are different, so carefully consult the official Sass JS API documentation. -
TypeError: Cannot read properties of undefined (reading 'css') (when using legacy API options)
cause Passing unsupported `outputStyle` values (other than 'expanded' or 'compressed'), `precision`, or `sourceComments` to the deprecated `render()`/`renderSync()` functions in Dart Sass's legacy API.fixRemove the `outputStyle` option if it's not 'expanded' or 'compressed'. Remove `precision` and `sourceComments` options entirely, as they are not supported by Dart Sass's legacy API and will cause errors. -
Cannot find module 'sass'
cause The 'sass' package is not installed, not listed in your project's dependencies, or cannot be resolved by your module loader.fixRun `npm install sass` or `yarn add sass` in your project directory. If using a global executable, ensure it's installed via `npm install -g sass` and your system's PATH variable is correctly configured. -
Error: Undefined variable.
cause A Sass variable was used without being defined in the current scope or an imported file.fixEnsure all variables are defined before use. Check for typos in variable names or verify that the file containing the variable definition is correctly imported using `@use` or `@forward` (preferred) or `@import` (legacy).
Warnings
- deprecated The legacy `render()` and `renderSync()` JavaScript API functions are deprecated and will be removed in Dart Sass 2.0.0. Projects should migrate to the modern `compile()` and `compileAsync()` APIs.
- gotcha The asynchronous `compileAsync()` function can be substantially slower than the synchronous `compile()` function when the input is local and not an I/O stream. For local file compilation, `compile()` may offer better performance, contrary to typical async/sync expectations in Node.js.
- breaking The legacy `render()` and `renderSync()` functions in Dart Sass have limitations compared to `node-sass`'s API. Specifically, they do not support `outputStyle` values other than 'expanded' or 'compressed', nor do they support `precision` or `sourceComments`. These options are explicitly unsupported.
- gotcha This `sass` package is a pure JavaScript distribution of *Dart Sass*. It is distinct from `node-sass`, which is a wrapper around LibSass (C++). While their legacy APIs were similar, their underlying implementations and feature evolution diverge. `node-sass` has been largely unmaintained for several years and may have compatibility issues.
Install
-
npm install sass -
yarn add sass -
pnpm add sass
Imports
- compile
import sass from 'sass'; const result = sass.compile(...);
import { compile } from 'sass'; - compileAsync
const sass = require('sass'); const result = sass.compileAsync(...);import { compileAsync } from 'sass'; - sass (namespace/default)
const sass = require('sass'); // While functional, ESM is the preferred module system for modern Node.js development.import * as sass from 'sass';
- Logger
import { Logger } from 'sass';
Quickstart
import { compile, compileAsync } from 'sass';
import * as fs from 'fs/promises';
import * as path from 'path';
async function runSassExample() {
const scssContent = `
$primary-color: #337ab7;
$font-stack: Helvetica, sans-serif;
body {
font: 100% $font-stack;
color: #333;
}
.button {
background-color: $primary-color;
color: white;
padding: 10px 15px;
border: none;
&:hover {
background-color: darken($primary-color, 10%);
}
}
`;
const tempScssFile = path.join(process.cwd(), 'styles.scss');
const tempCssFileSync = path.join(process.cwd(), 'styles-sync.css');
const tempCssFileAsync = path.join(process.cwd(), 'styles-async.css');
try {
await fs.writeFile(tempScssFile, scssContent);
console.log(`Wrote temporary SCSS to ${tempScssFile}`);
// Synchronous compilation (often faster for local files than async)
const syncResult = compile(tempScssFile);
await fs.writeFile(tempCssFileSync, syncResult.css);
console.log(`\nCompiled SCSS synchronously to ${tempCssFileSync}:\n${syncResult.css}`);
// Asynchronous compilation (generally preferred for non-blocking I/O, but can be slower for simple local files)
const asyncResult = await compileAsync(tempScssFile);
await fs.writeFile(tempCssFileAsync, asyncResult.css);
console.log(`\nCompiled SCSS asynchronously to ${tempCssFileAsync}:\n${asyncResult.css}`);
} catch (error) {
console.error('Sass compilation failed:', error);
} finally {
// Clean up temporary files
await fs.unlink(tempScssFile).catch(() => {});
await fs.unlink(tempCssFileSync).catch(() => {});
await fs.unlink(tempCssFileAsync).catch(() => {});
console.log('\nCleaned up temporary files.');
}
}
runSassExample();