Node-Sass JSON Importer

4.3.0 · abandoned · verified Sun Apr 19

node-sass-json-importer provides a mechanism to import JSON and JSON5 files directly into Sass stylesheets when processed by `node-sass`. It allows `@import` rules in Sass to resolve to `.json` or `.json5` files, making it possible to define design tokens, configuration variables, or other data structures in JSON and consume them directly within Sass. The package's last stable version was 4.3.0, released in May 2020. Given its fundamental dependency on the deprecated `node-sass` library, active development has ceased, and new releases are not expected. Its primary differentiator was bridging the gap between JSON data sources and Sass compilation workflows using `node-sass`, offering features like case conversion and custom path resolution, but it is not compatible with modern `sass` (Dart Sass) setups.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to integrate `node-sass-json-importer` with `node-sass` to process `.json` files within Sass, showing both asynchronous and synchronous `render` methods.

const sass = require('node-sass');
const jsonImporter = require('node-sass-json-importer');
const path = require('path');
const fs = require('fs');

const scssContent = `@import 'colors.json'; body { color: $primary-color; }`;
const jsonContent = `{ "primary-color": "red" }`;

// Create a dummy JSON file for the import
const jsonFilePath = path.join(__dirname, 'colors.json');
fs.writeFileSync(jsonFilePath, jsonContent);

sass.render({
  data: scssContent,
  importer: jsonImporter(), // Important: call jsonImporter() to get the function
}, function(err, result) {
  if (err) {
    console.error('Async compilation error:', err);
  } else {
    console.log('Compiled CSS (Async):\n', result.css.toString());
  }
  fs.unlinkSync(jsonFilePath); // Clean up
});

console.log('\n--- Synchronous compilation example ---\n');

const scssConfigContent = `@import 'config.json'; .my-element { font-size: $base-font-size; }`;
const jsonConfigContent = `{ "base-font-size": "16px" }`;
const jsonConfigPath = path.join(__dirname, 'config.json');
fs.writeFileSync(jsonConfigPath, jsonConfigContent);

try {
  const resultSync = sass.renderSync({
    data: scssConfigContent,
    importer: [jsonImporter()], // Can be an array of importers
  });
  console.log('Compiled CSS (Sync):\n', resultSync.css.toString());
} catch (e) {
  console.error('Sync compilation error:', e);
} finally {
  fs.unlinkSync(jsonConfigPath); // Clean up
}

view raw JSON →