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
muslnode 18–226 runs
build_error
glibcnode 18–226 runs
build_error
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
js_beautify
✓ import { js_beautify } from 'js-beautify';
✗ import beautify from 'js-beautify';
const beautify = require('js-beautify');
The primary JavaScript beautifier function is a named export. For CommonJS, use `require('js-beautify').js_beautify`.
css_beautify
✓ import { css_beautify } from 'js-beautify';
✗ const cssBeautify = require('js-beautify').css;
For CSS, `css_beautify` is a named export. Older CommonJS examples might show `require('js-beautify').css` but the `_beautify` suffix is standard.
html_beautify
✓ import { html_beautify } from 'js-beautify';
✗ const htmlBeautify = require('js-beautify').html;
Similar to CSS, `html_beautify` is the named export. Older CommonJS usage might use `require('js-beautify').html`.
This quickstart demonstrates how to use `js-beautify` to format JavaScript, CSS, and HTML strings with custom options.
import { js_beautify, css_beautify, html_beautify } from 'js-beautify';
const uglyJavaScript = `
function myFunction ( a , b ){return a + b ;}
`;
const uglyCSS = `
body{ margin:0;padding:0;}
`;
const uglyHTML = `
<div><span>Hello</span><p>World</p></div>
`;
const jsOptions = { indent_size: 2, space_in_empty_paren: true, keep_array_indentation: false };
const cssOptions = { indent_size: 4 };
const htmlOptions = { indent_size: 2, wrap_line_length: 80 };
const beautifulJavaScript = js_beautify(uglyJavaScript, jsOptions);
const beautifulCSS = css_beautify(uglyCSS, cssOptions);
const beautifulHTML = html_beautify(uglyHTML, htmlOptions);
console.log('--- Beautiful JavaScript ---\n', beautifulJavaScript);
console.log('--- Beautiful CSS ---\n', beautifulCSS);
console.log('--- Beautiful HTML ---\n', beautifulHTML);
/* Example Output:
-- Beautiful JavaScript ---
function myFunction (a, b) {
return a + b;
}
-- Beautiful CSS ---
body {
margin: 0;
padding: 0;
}
-- Beautiful HTML ---
<div>
<span>Hello</span>
<p>World</p>
</div>
*/
js-beautify --version
Debug
Known issues
gotchaThe `js-beautify` project is actively seeking new contributors due to limited time from existing maintainers. This could impact future development, bug fixes, and feature additions, suggesting a reliance on community contributions for its long-term vitality.fixConsider contributing to the project if you rely on it heavily, or be aware of potential slower updates and community-driven support.
affects: >=1.0.0
breakingVersion 1.15.1 included a hotfix to remove `polyfill.io` usage. If your projects were relying on older versions of `js-beautify` that implicitly used `polyfill.io`, this change might have impacted script loading or functionality if `polyfill.io` became unavailable or changed its behavior.fixUpdate to `js-beautify` v1.15.1 or newer to avoid `polyfill.io` dependency. Ensure your environment provides necessary polyfills if any were implicitly consumed via `polyfill.io` in older versions.
affects: <1.15.1
gotchaVersion 1.15.3 downgraded the `glob` dependency to v10.x to ensure compatibility with Node.js 18.x. Users on specific Node.js or `glob` versions might have encountered compatibility issues with earlier `js-beautify` releases that used newer `glob` versions.fixUpgrade to `js-beautify` v1.15.3 or later to ensure compatibility with Node.js 18.x, as the `glob` dependency has been adjusted for broader support.
affects: >=1.15.0 <1.15.3
Errors
Common errors & fixes
TypeError: js_beautify is not a function
Attempting to import `beautify` as a default export or using `require('js-beautify')` directly without specifying `js_beautify` when the library primarily uses named exports.
fixUse named imports: `import { js_beautify } from 'js-beautify';` for ESM, or `const { js_beautify } = require('js-beautify');` for CommonJS. Alternatively, for CommonJS, `const js_beautify = require('js-beautify').js_beautify;`. SyntaxError: Cannot use import statement outside a module
Using `import` syntax in a Node.js CommonJS module (e.g., in a `.js` file without `"type": "module"` in `package.json`).
fixEither convert your project or file to an ESM module by adding `"type": "module"` to your `package.json` or changing file extension to `.mjs`, or use CommonJS `require()` syntax: `const { js_beautify } = require('js-beautify');`. js-beautify isn't correctly formatting simple html
Attempting to use `js_beautify` function for HTML code, or not providing appropriate HTML-specific options.
fixEnsure you are using the correct beautifier function for the specific language: `html_beautify` for HTML and `css_beautify` for CSS. Pass relevant options for the chosen beautifier, as their options sets may differ.
Audit
Dependencies
No dependency data recorded yet.