Node-Sass JSON Importer
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
-
Error: 'A sentence with spaces.' is not a valid Sass string.
cause A JSON string containing spaces or special characters was not correctly escaped with inner single quotes, leading Sass to misinterpret it.fixIn your JSON file, ensure that values intended to be unquoted Sass strings containing special characters are wrapped in single quotes, e.g., change `"key": "A value with spaces"` to `"key": "'A value with spaces'"`. -
TypeError: jsonImporter is not a function
cause The imported `jsonImporter` variable, which is a factory function, was passed directly to the `importer` option without being called to produce the actual importer.fixEnsure you invoke the importer factory function: use `importer: jsonImporter()` instead of `importer: jsonImporter`. -
Module not found: Can't resolve 'node-sass-json-importer'
cause The package `node-sass-json-importer` is either not installed in your project, or the import path in a Webpack/bundler configuration is incorrect.fixFirst, run `npm install node-sass-json-importer` or `yarn add node-sass-json-importer`. Then, verify that the import statement or path in your `sass-loader` or `node-sass` configuration correctly points to the installed package.
Warnings
- breaking The underlying `node-sass` library, which this package depends on, is deprecated and no longer actively maintained. `node-sass-json-importer` is built specifically for `node-sass` and is not compatible with modern `sass` (Dart Sass) compilers.
- gotcha JSON string values that contain spaces, commas, or other special characters must be wrapped in *single quotes* within the *double-quoted JSON string itself* for Sass to interpret them correctly as unquoted Sass strings. Otherwise, Sass may throw parsing errors.
- deprecated Version 4.1.1 was officially marked as a 'Bad release' by the maintainers and should be avoided due to an issue with dependency handling.
- gotcha The `node-sass-json-importer` package exports a function that returns the actual importer function. Therefore, it must always be *called* (e.g., `jsonImporter()`) when passed to `node-sass`'s or `sass-loader`'s `importer` option.
Install
-
npm install node-sass-json-importer -
yarn add node-sass-json-importer -
pnpm add node-sass-json-importer
Imports
- jsonImporter
import jsonImporter from 'node-sass-json-importer';
const jsonImporter = require('node-sass-json-importer'); - jsonImporter
const jsonImporter = require('node-sass-json-importer').default;import jsonImporter from 'node-sass-json-importer';
- cli.js
./node_modules/.bin/node-sass --importer node_modules/node-sass-json-importer/dist/cli.js --recursive ./src --output ./dist
Quickstart
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
}