Registry / serialization / node-sass-json-functions

node-sass-json-functions

JSON →
library5.0.2jsnpmunverified

node-sass-json-functions provides utilities for JSON encoding and decoding within Sass stylesheets. Currently at stable version 5.0.2, the library typically sees updates tied to major Sass API changes or Node.js runtime requirements, with several releases over the past year. Its primary function is to bridge the gap between Sass data structures (lists, maps, colors, calculations, strings, numbers, booleans, nulls) and JSON strings. This allows developers to encode complex Sass data into JSON for use in CSS `content` properties, JavaScript interoperability, or other purposes, and subsequently decode JSON strings back into native Sass types. A key differentiator is its factory function approach (`createJsonFunctions`) that requires passing the `sass` or `sass-embedded` module reference, ensuring compatibility with the specific Sass implementation being used. This package is essential for projects needing to serialize or deserialize structured data directly within their Sass compilation process.

npm install node-sass-json-functions
INSTALL
IMPORT
SIG · NODE-SASS-JSON-FUN
N
node-sass-json-functions
serializationjavascriptv5.0.2
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.

createJsonFunctions
import createJsonFunctions from 'node-sass-json-functions';
const createJsonFunctions = require('node-sass-json-functions');
The module is ESM-only since v5.0.0. TypeScript types are included, so use this import style.

This snippet demonstrates how to integrate `node-sass-json-functions` into a Sass compilation process using `sass.compileAsync`. It shows how to pass the Sass implementation to the function factory to register `json-encode` and `json-decode` functions within your Sass environment.

import * as sass from 'sass'; // or 'sass-embedded' import createJsonFunctions from 'node-sass-json-functions'; // A minimal SCSS file for demonstration // Save this as 'index.scss' /* $list: 1, 2, '3', (4, 5, 6), (foo: 'bar baz'); $map: (foo: 1, bar: (2, 3)); body { content-list: json-encode($list); content-map: json-encode($map); content-decode-list: json-decode('[1,2,"test"]'); } */ (async () => { try { const result = await sass.compileAsync('./index.scss', { functions: { ...createJsonFunctions(sass) } }); console.log("Sass Compilation Result:"); console.log(result.css); // Expected minimal output: // body { // content-list: '[1,2,"3",[4,5,6],{"foo":"bar baz"}]'; // content-map: '{"foo":1,"bar":[2,3]}'; // content-decode-list: 1, 2, "test"; // } } catch (error) { console.error("Sass compilation failed:", error); } })();
Debug
Known issues
breakingVersion 5.0.0 introduced breaking changes, making the module ESM-only and requiring Node.js version 18 or higher. Projects must transition from CommonJS `require()` to ECMAScript Modules `import` statements.
fix
Update your import statements to use ECMAScript Modules (e.g., `import createJsonFunctions from 'node-sass-json-functions';`) and ensure your project's `package.json` specifies `"type": "module"` or uses `.mjs` files. Upgrade your Node.js runtime to at least v18.
affects: >=5.0.0
breakingSince version 5.0.0, the `createJsonFunctions` factory function now mandates passing the Sass implementation (`sass` or `sass-embedded` module reference) as an argument. Calling it without an argument will lead to errors.
fix
Adjust your usage from `createJsonFunctions()` to `createJsonFunctions(sass)` or `createJsonFunctions(sassEmbedded)` where `sass` or `sassEmbedded` is your imported Sass module.
affects: >=5.0.0
breakingVersion 4.0.0 moved to the new Sass API, dropping support for legacy Sass versions and requiring `sass` or `sass-embedded` version 1.45 or higher. For version 5.0.0 and above, the peer dependency further tightens to `sass` or `sass-embedded` version 1.97.3 or higher.
fix
Ensure your project uses `sass` or `sass-embedded` version 1.45 or newer (for `node-sass-json-functions` v4.x) or version 1.97.3 or newer (for `node-sass-json-functions` v5.x). Check your `package.json` for correct peer dependency resolution.
affects: >=4.0.0
Errors
Common errors & fixes
SyntaxError: Cannot use import statement outside a module
Attempting to use ESM-only module (`node-sass-json-functions` v5+) in a CommonJS environment.
fix
Ensure your project is configured for ESM by adding `"type": "module"` to your `package.json` or by using `.mjs` file extensions. Also, change `require()` to `import` statements.
TypeError: createJsonFunctions is not a function
This error often occurs when attempting to call `createJsonFunctions()` without passing the Sass implementation, as required by v5.0.0 and later.
fix
Pass the imported Sass module to the factory function, e.g., `functions: { ...createJsonFunctions(sass) }`.
SassError: Function json-encode is not found.
The `json-encode` or `json-decode` functions were not correctly registered with the Sass compilation process.
fix
Verify that `functions: { ...createJsonFunctions(sass) }` is correctly included within the options object passed to your `sass.compileAsync` or `sass.compile` call, and that `createJsonFunctions` received the `sass` (or `sass-embedded`) module as an argument.
Upgrade
Version history
5.0.2latest on npm
Audit
Dependencies
sassoptionalPeer dependency for Sass compilation functionality. Either 'sass' or 'sass-embedded' is required.
sass-embeddedoptionalPeer dependency for Sass compilation functionality. Either 'sass' or 'sass-embedded' is required.
Agent activity
2 hits · last 30 days
node
2
Resources
node-sass-json-functions — npm install node-sass-json-functions · libregistry