Sass (Pure JavaScript Implementation)

1.99.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates both synchronous (`compile`) and asynchronous (`compileAsync`) compilation of a simple SCSS string into CSS, writing the output to temporary files. It showcases common Sass features like variables and nesting, and cleans up after execution.

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();

view raw JSON →