The `css-variables-parser` library provides a focused utility for extracting CSS custom properties (variables) from CSS code. It offers both string-based parsing via the `parse` function and file-based parsing functionalities, including asynchronous (`parseFileAsync`, `parseFile`) and synchronous (`parseFileSync`) methods. The current stable version is 1.0.1. This library is specifically designed for scenarios requiring programmatic access to CSS variables, such as dynamic theme generation, build-time CSS processing, or integration with component libraries. A key differentiator is its optional support for custom CSS parsers, enabling integration with preprocessors like SugarSS. Its release cadence appears to be driven by specific bug fixes or minor feature enhancements rather than frequent major updates, reflecting its role as a dedicated parsing utility within the broader JavaScript ecosystem.
npm install css-variables-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates how to parse a CSS string to extract all defined custom properties and their values.
For browser environments, ensure you pass CSS as a string to the `parse` function. Pre-process CSS files on the server or during a build step if variables need to be extracted client-side.
Ensure your project's module system (ESM or CommonJS) aligns with how you import the library. For CommonJS, use `require()`. For ESM, use `import {} from '...'`. If using an older Node.js version, ensure 'type': 'module' is set in your package.json for ESM or stick to CommonJS syntax.Change your import statement from `const { parse } = require('css-variables-parser');` to `import { parse } from 'css-variables-parser';`.Verify that the file path is correct and accessible from where your Node.js application is running. Use absolute paths or `path.resolve()` for robustness.