# @bmewburn/js-beautify 中文文档

规范地址：https://npmcn.com/package/%40bmewburn/js-beautify/

版本：1\.15\.4
npm 元数据更新时间：2026-02-23T01:26:41.545Z
中文译文更新时间：2026-11-08T09:14:53.254Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40bmewburn/js-beautify
原文对照：https://npmcn.com/package/%40bmewburn/js-beautify/?lang=original
项目主页：https://beautifier.io/
源码仓库：https://github.com/beautifier/js-beautify

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：599 kB，35 个文件
- 模块格式：CommonJS
- TypeScript 类型：未声明
- Node.js 要求：\>=14
- 运行依赖：0 个
- 首次发布：2019-08-09T00:30:57.597Z
- 最新版本发布：2026-02-23T01:26:41.441Z
- 近 30 天下载量：73731（2026-10-09 至 2026-11-07，npm 官方下载统计）
- 已发布版本数：16（近 12 个月 2 个）

## 中文简介

beautifer.io 用于节点

## 安装

```sh
npm install @bmewburn/js-beautify
```

也可以使用：`pnpm add @bmewburn/js-beautify`、`yarn add @bmewburn/js-beautify`、`bun add @bmewburn/js-beautify`

## README

![JS Beautifier](https://raw.githubusercontent.com/beautifier/js-beautify/7db71fc/web/wordmark-light.svg)

[![CI](https://github.com/beautifier/js-beautify/workflows/CI/badge.svg)](https://github.com/beautifier/js-beautify/actions/workflows/main.yml) [![Greenkeeper badge](https://badges.greenkeeper.io/beautifier/js-beautify.svg)](https://greenkeeper.io/)

[![PyPI version](https://img.shields.io/pypi/v/jsbeautifier.svg)](https://pypi.python.org/pypi/jsbeautifier) [![CDNJS version](https://img.shields.io/cdnjs/v/js-beautify.svg)](https://cdnjs.com/libraries/js-beautify) [![NPM @latest](https://img.shields.io/npm/v/js-beautify.svg)](https://www.npmjs.com/package/js-beautify) [![NPM @next](https://img.shields.io/npm/v/js-beautify/next.svg)](https://www.npmjs.com/package/js-beautify?activeTab=versions)

[![Join the chat at https://gitter.im/beautifier/js-beautify](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/beautifier/js-beautify?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Twitter Follow](https://img.shields.io/twitter/follow/js_beautifier.svg?style=social&label=Follow)](https://twitter.com/intent/user?screen_name=js_beautifier)

[![NPM stats](https://nodei.co/npm/js-beautify.svg?downloadRank=true&downloads=true%22/)](https://www.npmjs.org/package/js-beautify)

这个小美人会重塑和重缩书签,丑陋 JavaScript,由迪恩·爱德华的受欢迎的打包工包装的无包装脚本, 以及由 npm 软件包处理的部分解析脚本 [javascript- 显示器](https://github.com/javascript-obfuscator/javascript-obfuscator)。 。 。 .

打开 [美化器.io](https://beautifier.io/) 试试看 选项可通过用户界面获取.

# 需要捐款者

我把这个前面和中央放在上面, 因为现有的业主现在在这个项目上工作的时间非常有限. 这是一个很受欢迎的项目,而且被广泛使用,但是它迫切需要那些有时间致力于修复两者的捐助者 客户面临错误以及内部设计和实施的根本问题.

如果你有兴趣,请看一下 [捐款额](https://github.com/beautifier/js-beautify/blob/main/CONTRIBUTING.md) 然后将问题固定为 ["好第一期"(第一期)](https://github.com/beautifier/js-beautify/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22) 标签并提交 PR。 越发重复. 谢谢!

# 安装

你可以为Node.js或Python安装美化器.

## 节点.js JavaScript

您可以安装国家防范机制软件包 `js-beautify`。在全球安装时,它提供了可执行文件 `js-beautify` 剧本. 和Python脚本一样,美化的结果被发送到 `stdout` 除非另有配置.

```bash
$ npm -g install js-beautify
$ js-beautify foo.js
```

您还可以使用 `js-beautify` 作为 `node` 库(在本地安装 `npm` 默认 :

```bash
$ npm install js-beautify
```

## 节点. js JavaScript (vNext)

以上安装最新稳定放出. 要安装β或RC版本 :

```bash
$ npm install js-beautify@next
```

## Web 图书馆

美人可以添加到您的网页上作为网页库.

JS Beautifer在两个CDN服务上提供: [(简体中文)](https://cdnjs.com/libraries/js-beautify) 和生猪.

要从其中一项服务中提取最新版本,文档中包含下面的一组脚本标记 :

```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.4/beautify.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.4/beautify-css.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.4/beautify-html.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.4/beautify.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.4/beautify-css.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.4/beautify-html.min.js"></script>
```

Html中JS标签的用法实例:

```html
<!DOCTYPE html>
<html lang="en">
  <body>

. . .

    <script src="https://cdnjs.cloudflare.com/ajax/libs/js-beautify/1.15.4/beautify.min.js"></script>
    <script src="script.js"></script>
  </body>
</html>
```

旧版本通过更改版本编号来提供.

免责声明:这些都是免费服务,所以有 [没有准备时间或支持保证](https://github.com/rgrove/rawgit/wiki/Frequently-Asked-Questions#i-need-guaranteed-100-uptime-should-i-use-cdnrawgitcom)。 。 。 .

## 蟒

要安装美化器的 Python 版本 :

```bash
$ pip install jsbeautifier
```

与JavaScript版本不同,Python版本只能重塑JavaScript. 与 HTML 或 CSS 文件无关, 但您可以安装 _css- 美化( c)_ 支助科:

```bash
$ pip install cssbeautifier
```

# 使用量

您可以在您的网页浏览器中使用 JS Beautifer 来美化 JavaScript, 或者在命令行中使用 Node.js 或 Python .

## 网页浏览器

打开 [美化器.io](https://beautifier.io/)。可通过用户界面提供选项.

## Web 图书馆

装入后 `<script>` 在您的标签 `html` 文件,它们会显示三种功能: `js_beautify`, (中文(简体) ), `css_beautify`,以及 `html_beautify`

美化 json 字符串的用法示例 :

```js
const options = { indent_size: 2, space_in_empty_paren: true }

const dataObj = {completed: false,id: 1,title: "delectus aut autem",userId: 1,}

const dataJson = JSON.stringify(dataObj)

js_beautify(dataJson, options)

/* OUTPUT
{
  "completed": false,
  "id": 1,
  "title": "delectus aut autem",
  "userId": 1,
}
*/
```

## 节点.js JavaScript

全球安装时, 美容器提供可执行文件 `js-beautify` 剧本. 美容结果发给 `stdout` 除非另有配置.

```bash
$ js-beautify foo.js
```

用于 `js-beautify` 作为 `node` 库(在本地安装后),导入并调用JavaScript(JS),CSS,或HTML的适当美化方法. 所有三种方法签名都是 `beautify(code, options)`。 。 。 . `code` 是要被美化的代码串。 选项是带有您想要用来美化代码的设置的对象.

配置选项名称与 CLI 名称相同, 但带有下划线而不是破折号 。 譬如说, `--indent-size 2 --space-in-empty-paren` 这将是 `{ indent_size: 2, space_in_empty_paren: true }`。 。 。 .

```js
var beautify = require('js-beautify/js').js,
    fs = require('fs');

fs.readFile('foo.js', 'utf8', function (err, data) {
    if (err) {
        throw err;
    }
    console.log(beautify(data, { indent_size: 2, space_in_empty_paren: true }));
});
```

如果您正在使用ESM导入, 您可以导入 `js-beautify` 像这样:

```js
// 'beautify' can be any name here.
import beautify from 'js-beautify';

beautify.js(data, options);
beautify.html(data, options);
beautify.css(data, options);
```

## 蟒

安装后, 使用 Python 美化 :

```bash
$ js-beautify file.js
```

美化输出到 `stdout` 默认.

用于 `jsbeautifier` 作为图书馆很简单:

```python
import jsbeautifier
res = jsbeautifier.beautify('your JavaScript string')
res = jsbeautifier.beautify_file('some_file.js')
```

...或者,要指定一些选项:

```python
opts = jsbeautifier.default_options()
opts.indent_size = 2
opts.space_in_empty_paren = True
res = jsbeautifier.beautify('some JavaScript', opts)
```

配置选项名称与 CLI 名称相同, 但带有下划线而不是破折号 。 上述例子将设置在命令行上,作为 `--indent-size 2 --space-in-empty-paren`。 。 。 .

# 选项

这是Python和JS剧本的指挥线旗:

```text
CLI Options:
  -f, --file       Input file(s) (Pass '-' for stdin)
  -r, --replace    Write output in-place, replacing input
  -o, --outfile    Write output to file (default stdout)
  --config         Path to config file
  --type           [js|css|html] ["js"] Select beautifier type (NOTE: Does *not* filter files, only defines which beautifier type to run)
  -q, --quiet      Suppress logging to stdout
  -h, --help       Show this help
  -v, --version    Show the version

Beautifier Options:
  -s, --indent-size                 Indentation size [4]
  -c, --indent-char                 Indentation character [" "]
  -t, --indent-with-tabs            Indent with tabs, overrides -s and -c
  -e, --eol                         Character(s) to use as line terminators.
                                    [first newline in file, otherwise "\n]
  -n, --end-with-newline            End output with newline
  --editorconfig                    Use EditorConfig to set up the options
  -l, --indent-level                Initial indentation level [0]
  -p, --preserve-newlines           Preserve line-breaks (--no-preserve-newlines disables)
  -m, --max-preserve-newlines       Number of line-breaks to be preserved in one chunk [10]
  -P, --space-in-paren              Add padding spaces within paren, ie. f( a, b )
  -E, --space-in-empty-paren        Add a single space inside empty paren, ie. f( )
  -j, --jslint-happy                Enable jslint-stricter mode
  -a, --space-after-anon-function   Add a space before an anonymous function's parens, ie. function ()
  --space-after-named-function      Add a space before a named function's parens, i.e. function example ()
  -b, --brace-style                 [collapse|expand|end-expand|none][,preserve-inline] [collapse,preserve-inline]
  -u, --unindent-chained-methods    Don't indent chained method calls
  -B, --break-chained-methods       Break chained method calls across subsequent lines
  -k, --keep-array-indentation      Preserve array indentation
  -x, --unescape-strings            Decode printable characters encoded in xNN notation
  -w, --wrap-line-length            Wrap lines that exceed N characters [0]
  -X, --e4x                         Pass E4X xml literals through untouched
  --good-stuff                      Warm the cockles of Crockford's heart
  -C, --comma-first                 Put commas at the beginning of new line instead of end
  -O, --operator-position           Set operator position (before-newline|after-newline|preserve-newline) [before-newline]
  --indent-empty-lines              Keep indentation on empty lines
  --templating                      List of templating languages (auto,django,erb,handlebars,php,smarty,angular) ["auto"] auto = none in JavaScript, all in HTML
```

对应两个库界面下划线选项密钥

**每个 CLI 选项默认值**

```json
{
    "indent_size": 4,
    "indent_char": " ",
    "indent_with_tabs": false,
    "editorconfig": false,
    "eol": "\n",
    "end_with_newline": false,
    "indent_level": 0,
    "preserve_newlines": true,
    "max_preserve_newlines": 10,
    "space_in_paren": false,
    "space_in_empty_paren": false,
    "jslint_happy": false,
    "space_after_anon_function": false,
    "space_after_named_function": false,
    "brace_style": "collapse",
    "unindent_chained_methods": false,
    "break_chained_methods": false,
    "keep_array_indentation": false,
    "unescape_strings": false,
    "wrap_line_length": 0,
    "e4x": false,
    "comma_first": false,
    "operator_position": "before-newline",
    "indent_empty_lines": false,
    "templating": ["auto"]
}
```

**默认未在 cli 中曝光**

```json
{
  "eval_code": false,
  "space_before_conditional": true
}
```

通知并非所有的默认都通过 CLI 被曝光 。 历史上,Python和 JS API并非完全相同. 还有一个 很少有其他案例 阻止我们100%的API兼容性.

## 从环境或.jsbeautifilic( JavaScript- Only) 装入设置

除了CLI的论据外,您还可以通过下列途径向JS可执行文件传递配置:

-   任意 `jsbeautify_`\- 前置环境变量
-   (单位:千美元) `JSON`\- 格式化文件,由 `--config` 参数
-   (单位:千美元) `.jsbeautifyrc` 包含的文件 `JSON` 以上文件系统任何级别的数据 `$PWD`

此堆栈中早先提供的配置源将覆盖后面的 .

## 设置继承和特定语言的覆盖

这些设置是浅树,其价值被所有语言所继承,但是 可以被推翻。 这适用于在实施中直接传递给API的设置. 在JavaScript执行中,从一个配置文件中加载的设置,如.jsbeautificrc,也可以使用继承/覆盖.

下面是显示所有支持位置的示例配置树 用于语言覆盖节点。 我们用 `indent_size` 讨论此配置将如何操作,但任何数量的设置都可以被继承或覆盖:

```json
{
    "indent_size": 4,
    "html": {
        "end_with_newline": true,
        "js": {
            "indent_size": 2
        },
        "css": {
            "indent_size": 2
        }
    },
    "css": {
        "indent_size": 1
    },
    "js": {
       "preserve-newlines": true
    }
}
```

采用上述例子可以得出以下结果:

-   HTML 文件
    -   继承 `indent_size` 来自顶层设置的4个空格.
    -   文件也会以新行结束 .
    -   HTML 内部的 JavaScript 和 CSS
        -   继承 HTML `end_with_newline` 设置.
        -   将缩入2个空间.
-   CSS 文件
    -   将顶层设置覆盖到 `indent_size` 1个空间.
-   JavaScript 文件
    -   继承 `indent_size` 来自顶层设置的4个空格.
    -   设定 `preserve-newlines` 改为 `true`。 。 。 .

## CSS 和 HTML 软件

计外 `js-beautify` 可执行文件, `css-beautify` 财务报告和审定财务报表 `html-beautify` 还提供了这些脚本的简单界面。 或者, `js-beautify --css` 或者说 `js-beautify --html` 将分别完成同样的任务.

```js
// Programmatic access
var beautify_js = require('js-beautify'); // also available under "js" export
var beautify_css = require('js-beautify').css;
var beautify_html = require('js-beautify').html;

// All methods accept two arguments, the string to be beautified, and an options object.
```

CSS & HTML 美化器在范围上要简单得多,拥有的选项要少得多.

```text
CSS Beautifier Options:
  -s, --indent-size                  Indentation size [4]
  -c, --indent-char                  Indentation character [" "]
  -t, --indent-with-tabs             Indent with tabs, overrides -s and -c
  -e, --eol                          Character(s) to use as line terminators. (default newline - "\\n")
  -n, --end-with-newline             End output with newline
  -b, --brace-style                  [collapse|expand] ["collapse"]
  -L, --selector-separator-newline   Add a newline between multiple selectors
  -N, --newline-between-rules        Add a newline between CSS rules
  --indent-empty-lines               Keep indentation on empty lines

HTML Beautifier Options:
  -s, --indent-size                  Indentation size [4]
  -c, --indent-char                  Indentation character [" "]
  -t, --indent-with-tabs             Indent with tabs, overrides -s and -c
  -e, --eol                          Character(s) to use as line terminators. (default newline - "\\n")
  -n, --end-with-newline             End output with newline
  -p, --preserve-newlines            Preserve existing line-breaks (--no-preserve-newlines disables)
  -m, --max-preserve-newlines        Maximum number of line-breaks to be preserved in one chunk [10]
  -I, --indent-inner-html            Indent <head> and <body> sections. Default is false.
  -b, --brace-style                  [collapse-preserve-inline|collapse|expand|end-expand|none] ["collapse"]
  -S, --indent-scripts               [keep|separate|normal] ["normal"]
  -w, --wrap-line-length             Maximum characters per line (0 disables) [250]
  -A, --wrap-attributes              Wrap attributes to new lines [auto|force|force-aligned|force-expand-multiline|aligned-multiple|preserve|preserve-aligned] ["auto"]
  -M, --wrap-attributes-min-attrs    Minimum number of html tag attributes for force wrap attribute options [2]
  -i, --wrap-attributes-indent-size  Indent wrapped attributes to after N characters [indent-size] (ignored if wrap-attributes is "aligned")
  -d, --inline                       List of tags to be considered inline tags
  --inline_custom_elements           Inline custom elements [true]
  -U, --unformatted                  List of tags (defaults to inline) that should not be reformatted
  -T, --content_unformatted          List of tags (defaults to pre) whose content should not be reformatted
  -E, --extra_liners                 List of tags (defaults to [head,body,/html] that should have an extra newline before them.
  --editorconfig                     Use EditorConfig to set up the options
  --indent_scripts                   Sets indent level inside script tags ("normal", "keep", "separate")
  --unformatted_content_delimiter    Keep text content together between this string [""]
  --indent-empty-lines               Keep indentation on empty lines
  --templating                       List of templating languages (auto,none,django,erb,handlebars,php,smarty,angular) ["auto"] auto = none in JavaScript, all in html
```

## 指令

指令允许您从源文件中控制 Beautier 的行为 。 指令被放在文件内部的评论中. 指令的格式 `/* beautify {name}:{value} */` 在 CSS 和 JavaScript 中。 HTML 格式化为 `<!-- beautify {name}:{value} -->`。 。 。 .

### 忽略指令

这个 `ignore` 指令使美化器完全忽略了文件的部分内容,将其作为未解析的文字处理. 以下投入在美化后保持不变:

```js
// Use ignore when the content is not parsable in the current language, JavaScript in this case.
var a =  1;
/* beautify ignore:start */
 {This is some strange{template language{using open-braces?
/* beautify ignore:end */
```

### 保存指令

注:此指令只在 HTML 和 JavaScript 中有效,而不是 CSS .

这个 `preserve` 指令使Beautifier 解析,然后保留代码一节的现有格式.

以下投入在美化后保持不变:

```js
// Use preserve when the content is valid syntax in the current language, JavaScript in this case.
// This will parse the code and preserve the existing formatting.
/* beautify preserve:start */
{
    browserName: 'internet explorer',
    platform:    'Windows 7',
    version:     '8'
}
/* beautify preserve:end */
```

# 许可证

您可以随意使用此软件, 以防您发现此软件有用或为您工作, 但您必须保留版权通知和许可 。 (麻省理工学院)

# 贷项

-   由艾纳·利尔马尼斯创造, [einar@ beautifer.io](mailto:einar@beautifier.io)
-   Python 版本由 Stefano Sanfilippo 所繁荣 [a. little.coder@gmail.com (中文(简体) )](mailto:a.little.coder@gmail.com)
-   丹尼尔·斯托克曼的节点J命令线 [丹尼尔·斯托克曼@gmail.com](mailto:daniel.stockman@gmail.com)
-   由Liam Newman维护和扩建 [比特睿曼@ beautifer.io](mailto:bitwiseman@beautifier.io)

还感谢杰森·戴蒙德,帕特里克·霍夫,诺克洪·索松克,安德烈亚斯·施耐德,戴夫 瓦西里耶夫斯基 维塔利·蝙蝠侠诺夫 罗恩·鲍德温 加布里埃尔·哈里森 克里斯·J·舒尔, 马蒂亚斯·拜恩斯,维托里奥·甘巴莱塔等.

(README.MD: (英语) [js- beautify@1.15.4 (法语)](mailto:js-beautify@1.15.4)(中文(简体) )

## 常见问题

### @bmewburn/js-beautify 是什么？

beautifer\.io 用于节点

### 如何安装 @bmewburn/js-beautify？

在项目目录运行 npm install @bmewburn/js-beautify，或使用 pnpm add / yarn add / bun add。

### @bmewburn/js-beautify 支持 TypeScript 吗？

该包的 package\.json 没有声明类型入口。需要类型提示时，可以在 npm 上查找社区维护的 @types/bmewburn\_\_js-beautify 是否存在。

### @bmewburn/js-beautify 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @bmewburn/js-beautify 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=14。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @bmewburn/js-beautify 可以商用吗？

@bmewburn/js-beautify 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @bmewburn/js-beautify 的下载量有多少？

根据 npm 官方下载统计，@bmewburn/js-beautify 在 2026/10/09 – 2026/11/07 这 30 天内被下载 73,731 次，日均约 2,458 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @bmewburn/js-beautify 还在维护吗？

最新版本 1\.15\.4 发布于 2026/02/23（8 个月前）；至今共发布 16 个版本，其中过去 12 个月发布了 2 个；首次发布于 2019/08/09。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

