Registry / serialization / css-parse

css-parse

JSON →
library2.0.0jsnpmunverified

css-parse is a JavaScript CSS parser originally designed for Node.js, primarily serving as a thin wrapper that exposes the `parse` method from the `css` (reworkcss/css) package. Published over a decade ago (June 2014) at its 2.0.0 version, it has not seen further updates. The underlying `reworkcss/css` library, while slightly more recent (last updated July 2020), is also largely part of an inactive ecosystem of CSS preprocessing tools (`reworkcss`). This package does not support modern CSS syntax out-of-the-box and is exclusively CommonJS. For contemporary CSS parsing, developers typically use actively maintained libraries like PostCSS, which offer broader features, plugin ecosystems, and support for the latest CSS specifications and module systems.

npm install css-parse
INSTALL
IMPORT
SIG · CSS-PARSE
C
css-parse
serializationjavascriptv2.0.0
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.

parse
const parse = require('css-parse')
import { parse } from 'css-parse'
This package is CommonJS-only. ES module imports are not supported.
parse (from css)
const { parse } = require('css')
import { parse } from 'css'
While 'css-parse' re-exports `parse`, it's often more direct to import from 'css' directly, which is also CJS-only.

Demonstrates parsing a CSS string into an Abstract Syntax Tree (AST) using the `parse` method.

const parse = require('css-parse'); const cssString = ` /* Example CSS */ body { font-family: Arial, sans-serif; color: #333; } .container { margin: 0 auto; max-width: 960px; } @media (max-width: 600px) { .container { padding: 10px; } } `; try { const ast = parse(cssString, { source: 'example.css' }); console.log('Parsed AST:', JSON.stringify(ast, null, 2)); // To access rules, e.g.: // console.log(ast.stylesheet.rules[0].selectors); // console.log(ast.stylesheet.rules[0].declarations); } catch (error) { console.error('Parsing error:', error.message); }
Debug
Known issues
breakingThis package and its underlying 'css' dependency are part of the 'reworkcss' ecosystem, which is largely unmaintained. It may not correctly parse or handle modern CSS features and syntax (e.g., CSS Custom Properties, newer pseudo-classes/elements, nesting, 'gap' for flexbox/grid).
fix
Consider migrating to an actively maintained CSS parser like PostCSS or a CSS-in-JS solution for robust and up-to-date CSS processing.
affects: >=1.0.0
gotchaThe package is CommonJS-only and does not support ES module syntax (`import/export`). Attempting to use it in an ESM context without proper transpilation will result in errors.
fix
Use `require()` for importing. If you must use it in an ESM project, consider dynamic `import()` or a CJS wrapper, but re-evaluate if a modern ESM-compatible parser is a better fit.
affects: >=1.0.0
gotchaBy default, `css.parse` (which `css-parse` uses) throws an error on invalid CSS. To collect errors without stopping execution, you must pass the `{ silent: true }` option and check `ast.stylesheet.parsingErrors`.
fix
When parsing potentially invalid CSS, pass `{ silent: true }` as an option: `const ast = parse(cssString, { silent: true }); if (ast.stylesheet.parsingErrors) { console.error(ast.stylesheet.parsingErrors); }`
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: parse is not a function
Incorrectly importing the module as a default export or attempting named import from a CommonJS module in an ESM context.
fix
Ensure you are using `const parse = require('css-parse');` for CommonJS environments.
SyntaxError: Unexpected token 'export'
Attempting to use `css-parse` (or its `css` dependency) with ES module syntax (`import`) in a pure ESM environment without a CommonJS compatibility layer.
fix
Modify your import to `const parse = require('css-parse');` or configure your build system to handle CommonJS modules in an ESM context.
SyntaxError: missing '}' (CSS)
The input CSS string contains a syntax error, and `css-parse` is throwing an error by default (not in 'silent' mode).
fix
Either correct the CSS syntax or parse with the `{ silent: true }` option to collect errors in `ast.stylesheet.parsingErrors` instead of throwing.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies
cssrequiredThis package exports the `parse` method from the 'css' package.
Agent activity
2 hits · last 30 days
node
2
Resources
css-parse — npm install css-parse · libregistry