# strip-json-comments 中文文档

规范地址：https://npmcn.com/package/strip-json-comments/

版本：5\.0\.3
npm 元数据更新时间：2025-08-08T14:14:05.184Z
中文译文更新时间：2026-11-04T14:01:58.554Z
软件包许可：MIT

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

原始 npm 页面：https://www.npmjs.com/package/strip-json-comments
原文对照：https://npmcn.com/package/strip-json-comments/?lang=original
项目主页：https://github.com/sindresorhus/strip-json-comments#readme
源码仓库：https://github.com/sindresorhus/strip-json-comments

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

- 包体积（解压后）：8.2 kB，5 个文件
- 模块格式：ES Module
- TypeScript 类型：包内自带
- Node.js 要求：\>=14\.16
- 运行依赖：0 个
- 首次发布：2013-11-17T15:19:04.627Z
- 最新版本发布：2025-08-08T14:14:05.044Z
- 近 30 天下载量：843920858（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：20（近 12 个月 0 个）

## 中文简介

从JSON中删除注释。让你在JSON文件中使用注释！

## 安装

```sh
npm install strip-json-comments
```

也可以使用：`pnpm add strip-json-comments`、`yarn add strip-json-comments`、`bun add strip-json-comments`

## README

# strip-json-comments

> 从JSON中删除注释。让你在JSON文件中使用注释！

现在这是可能的：

```js
{
    // Rainbows
    "unicorn": /* ❤ */ "cake"
}
```

它将取代单行注释 `//` 还有多行注释 `/**/` 与空白。这允许JSON错误位置尽可能地保持在原始源的附近。

也可作为 [狼吞虎咽地吃](https://github.com/sindresorhus/gulp-strip-json-comments)/[咕哝着说](https://github.com/sindresorhus/grunt-strip-json-comments)/[西兰花](https://github.com/sindresorhus/broccoli-strip-json-comments) 插件。

## 安装

```sh
npm install strip-json-comments
```

## 用法

```js
import stripJsonComments from 'strip-json-comments';

const json = `{
    // Rainbows
    "unicorn": /* ❤ */ "cake"
}`;

JSON.parse(stripJsonComments(json));
//=> {unicorn: 'cake'}
```

## API

### stripJsonComments (jsonString、期权?)

#### jsonString

类型: `string`

接受带有JSON的字符串并返回不带注释的字符串。

#### 选项

类型: `object`

##### trailingCommas

类型: `boolean`  
默认值: `false`

除去注释后面的逗号。

##### 空格

类型: `boolean`  
默认值: `true`

用空白替换注释和后面的逗号，而不是完全去掉它们。

## 基准

```sh
npm run bench
```

## 相关

-   [strip-json-comments-cli](https://github.com/sindresorhus/strip-json-comments-cli) —该模块的CLI
-   [strip-css-comments](https://github.com/sindresorhus/strip-css-comments) -从CSS中删除注释

## 常见问题

### strip-json-comments 是什么？

从JSON中删除注释。让你在JSON文件中使用注释！

### 如何安装 strip-json-comments？

在项目目录运行 npm install strip-json-comments，或使用 pnpm add / yarn add / bun add。

### strip-json-comments 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### strip-json-comments 支持 ESM 还是 CommonJS？

以 ES Module 形式发布，推荐使用 import 引入。在 CommonJS 项目中，Node\.js 20\.19\+ / 22\.12\+ 可以直接 require\(\) 不含顶层 await 的 ES Module，更早的版本需要改用动态 import\(\)。

### strip-json-comments 需要什么版本的 Node\.js？

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

### strip-json-comments 可以商用吗？

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

### strip-json-comments 的下载量有多少？

根据 npm 官方下载统计，strip-json-comments 在 2026/10/05 – 2026/11/03 这 30 天内被下载 843,920,858 次，日均约 28,130,695 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### strip-json-comments 还在维护吗？

最新版本 5\.0\.3 发布于 2025/08/08（1\.2 年前）；至今共发布 20 个版本，其中过去 12 个月发布了 0 个；首次发布于 2013/11/17。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

