JS Beautify
JS Beautify is a robust JavaScript library and command-line tool designed for reformatting and re-indenting JavaScript, CSS, and HTML code. It can also unpack and partially deobfuscate scripts processed by common tools like Dean Edward’s packer or `javascript-obfuscator`. The current stable version is 1.15.4, with recent releases addressing compatibility and hotfixes, such as the `polyfill.io` removal and `glob` dependency adjustments for Node.js 18.x. The project distinguishes itself by offering multi-language code formatting and basic de-obfuscation capabilities, which are beyond the scope of many single-purpose formatters. However, the project explicitly states a critical need for new contributors, indicating that existing owners have limited time, which suggests a potentially slower development pace and a maintenance-focused trajectory despite ongoing minor updates.
Common errors
-
TypeError: js_beautify is not a function
cause 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
cause 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
cause 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.
Warnings
- gotcha The `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.
- breaking Version 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.
- gotcha Version 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.
Install
-
npm install js-beautify -
yarn add js-beautify -
pnpm add js-beautify
Imports
- js_beautify
import beautify from 'js-beautify'; const beautify = require('js-beautify');import { js_beautify } from 'js-beautify'; - css_beautify
const cssBeautify = require('js-beautify').css;import { css_beautify } from 'js-beautify'; - html_beautify
const htmlBeautify = require('js-beautify').html;import { html_beautify } from 'js-beautify';
Quickstart
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>
*/