mensch

raw JSON →
0.3.4 verified Sat Apr 25 auth: no javascript maintenance

A decent, non-validating CSS parser and stringifier. Current stable version is 0.3.4 (last release 2015-04-02, now in maintenance mode). Provides parse, stringify, and lex functions with support for comments and position tracking. Unlike validating parsers, mensch handles malformed CSS (mis-matched braces, missing semi-colons) without error. Differentiators: comments in AST, position info, and lenient parsing. Alternatives: PostCSS, css, csstree.

error TypeError: mensch.parse is not a function
cause Importing mensch incorrectly, e.g., import parse from 'mensch' instead of import mensch from 'mensch'.
fix
Use import mensch from 'mensch'; then mensch.parse().
error Cannot find module 'mensch'
cause Package not installed or typo in package name.
fix
Run npm install mensch and ensure correct casing.
error Unexpected token ILLEGAL
cause CSS contains modern syntax like CSS custom properties (var(--foo)) or nesting that mensch cannot parse.
fix
Switch to a modern parser like PostCSS.
gotcha Mensch is a non-validating parser. It will not flag invalid CSS like misspelled property names or misplaced @import.
fix Use a validating parser (e.g., PostCSS with stylelint) if validation is needed.
gotcha Comments within selectors, properties, or values are silently ignored. They do not appear in the AST even with {comments: true}.
fix Keep comments only between rules or declarations.
deprecated The package has no new releases since 2015. It is effectively in maintenance mode; no updates for modern CSS features (e.g., custom properties, nesting).
fix Consider using PostCSS or csstree for modern CSS parsing.
gotcha Global leak in v0.3.3 and earlier: the parser could leak variables into global scope.
fix Update to v0.3.4 or later.
gotcha When using CommonJS require(), the default export is the whole module, not an object with methods. Use const mensch = require('mensch'); then mensch.parse().
fix Use const mensch = require('mensch'); or import * as mensch from 'mensch'.
npm install mensch
yarn add mensch
pnpm add mensch

Parse a CSS string with comments and positions, then stringify with indentation.

import mensch from 'mensch';

const css = 'p { color: black; }';
const ast = mensch.parse(css, { comments: true, position: true });
console.log(JSON.stringify(ast, null, 2));
const output = mensch.stringify(ast, { comments: true, indentation: '  ' });
console.log(output);