Lightning CSS (Linux x64 Musl)
lightningcss is a high-performance CSS parser, transformer, minifier, and bundler written in Rust, designed for speed and efficiency in web development workflows. It provides advanced features like CSS module scoping, browser compatibility targeting, and experimental CSS feature transpilation. This particular package, `lightningcss-linux-x64-musl`, is a platform-specific native addon providing the core Rust-based functionality for Linux x64 systems using the musl libc implementation. It is typically installed as an optional dependency of the main `lightningcss` package, which then dynamically loads the correct native binding for the host environment. The library is actively maintained, with frequent minor releases (often monthly or bi-monthly) incorporating new CSS features, performance improvements, and bug fixes, as evidenced by recent updates to v1.32.0, v1.31.0, and v1.30.0. Its key differentiators are its Rust-native performance, comprehensive CSS spec support, and bundling capabilities.
Common errors
-
Error: Cannot find module 'lightningcss' or its corresponding type declarations.
cause Attempting to `import` or `require` `lightningcss` when it's not installed or incorrectly resolved. This specific package (`lightningcss-linux-x64-musl`) is a native binding, not the main JavaScript API.fixEnsure `npm install lightningcss` has been run. The main `lightningcss` package will then correctly pull in the platform-specific native dependency. -
Error: Failed to load native binding
cause The JavaScript wrapper in `lightningcss` could not find or load the appropriate native binary for your system, or the binary itself is corrupted/incompatible.fixThis often indicates a mismatch between your system's architecture/OS/libc and the installed native `lightningcss-*-*` package. Try `npm rebuild lightningcss` or delete `node_modules` and `package-lock.json` then reinstall. Verify your Node.js version meets the `engines` requirement (Node.js >= 12.0.0). -
TypeError: Cannot read properties of undefined (reading 'transform')
cause This error occurs if the `transform` function is not properly imported or the `lightningcss` module did not initialize correctly, likely due to a failure in loading the native binding.fixFirst, address any 'Failed to load native binding' errors. Ensure your `import { transform } from 'lightningcss';` statement is correct and that the main `lightningcss` package is installed and functional. -
Error: The `code` option must be a Buffer.
cause The `transform` and `bundle` functions in `lightningcss` expect the `code` option to be a Node.js `Buffer` object, not a string.fixConvert your CSS string to a Buffer before passing it: `code: Buffer.from(yourCssString)`.
Warnings
- gotcha This package, `lightningcss-linux-x64-musl`, is a platform-specific native addon. It is generally not intended for direct use but should be installed as an `optionalDependency` or `peerDependency` of the main `lightningcss` package. Direct installation of only this package without `lightningcss` will not provide the JavaScript API.
- breaking In v1.30.0, the relative color parsing was updated to the latest CSS spec. This change means `calc()` functions within colors now always resolve to numbers instead of percentages, and numbers are supported in addition to percentages. If your CSS uses relative color calculations with percentages, you may need to update them to use numbers instead.
- gotcha Lightning CSS relies on native binaries. If you encounter errors related to missing shared libraries or incorrect architecture, ensure you have the correct platform-specific package (e.g., `-linux-x64-gnu`, `-win32-x64`, `-darwin-arm64`) installed. The `detect-libc` library is used to choose the correct `libc` variant (e.g., `musl` vs. `gnu`) on Linux.
- breaking The nesting implementation was updated in v1.30.0 to align with the new CSS Nesting spec, allowing selectors with pseudo-elements and more flexible nesting of declarations and rules. While generally an enhancement, this might subtly change how previously supported but non-standard nesting patterns are parsed or interpreted.
Install
-
npm install lightningcss-linux-x64-musl -
yarn add lightningcss-linux-x64-musl -
pnpm add lightningcss-linux-x64-musl
Imports
- transform
const { transform } = require('lightningcss');import { transform } from 'lightningcss'; - bundle
import { bundle } from 'lightningcss'; - browserslistToTargets
import { browserslistToTargets } from 'lightningcss';
Quickstart
import { transform, browserslistToTargets } from 'lightningcss';
import * as browserslist from 'browserslist';
const cssInput = `
@import "./base.css";
:root {
--primary-color: #007bff;
}
.container {
display: flex;
gap: 10px;
@media (min-width: 768px) {
flex-direction: row;
}
@media (max-width: 767px) {
flex-direction: column;
}
}
.button {
background-color: var(--primary-color);
color: white;
padding: 10px 20px;
border-radius: 5px;
}
`;
const targets = browserslistToTargets(browserslist('>= 0.25%'));
async function processCss() {
try {
const { code, map } = await transform({
filename: 'input.css',
code: Buffer.from(cssInput),
minify: true,
sourceMap: true,
targets,
cssModules: false,
// Example custom resolver for @import (synchronous)
resolver: {
resolve(specifier, from) {
if (specifier === './base.css') {
return {
filePath: 'base.css',
contents: Buffer.from('body { margin: 0; }')
};
}
return null;
}
}
});
console.log('Transformed CSS:\n', code.toString());
// console.log('Source Map:\n', map?.toString());
} catch (e) {
console.error('Error transforming CSS:', e);
}
}
processCss();