json-parse-even-better-errors

中文文档
6.0.0发布于 2026/05/09MITCommonJS零依赖构建来源已验证近 30 天下载 4.6亿

JSON.parse with context information on error

原始使用文档
文档目录6 节
  1. Install
  2. Table of Contents
  3. Example
  4. Features
  5. Indentation
  6. API

json-parse-even-better-errors

json-parse-even-better-errors is a Node.js library for getting nicer errors out of JSON.parse(), including context and position of the parse errors.

It also preserves the newline and indentation styles of the JSON data, by putting them in the object or array in the Symbol.for('indent') and Symbol.for('newline') properties.

Install

$ npm install --save json-parse-even-better-errors

Table of Contents

Example

const parseJson = require('json-parse-even-better-errors')

parseJson('"foo"') // returns the string 'foo'
parseJson('garbage') // more useful error message
parseJson.noExceptions('garbage') // returns undefined

Features

  • Like JSON.parse, but the errors are better.
  • Strips a leading byte-order-mark that you sometimes get reading files.
  • Has a noExceptions method that returns undefined rather than throwing.
  • Attaches the newline character(s) used to the Symbol.for('newline') property on objects and arrays.
  • Attaches the indentation character(s) used to the Symbol.for('indent') property on objects and arrays.

Indentation

To preserve indentation when the file is saved back to disk, use data[Symbol.for('indent')] as the third argument to JSON.stringify, and if you want to preserve windows \r\n newlines, replace the \n chars in the string with data[Symbol.for('newline')].

For example:

const txt = await readFile('./package.json', 'utf8')
const data = parseJsonEvenBetterErrors(txt)
const indent = Symbol.for('indent')
const newline = Symbol.for('newline')
// .. do some stuff to the data ..
const string = JSON.stringify(data, null, data[indent]) + '\n'
const eolFixed = data[newline] === '\n' ? string
  : string.replace(/\n/g, data[newline])
await writeFile('./package.json', eolFixed)

Indentation is determined by looking at the whitespace between the initial { and [ and the character that follows it. If you have lots of weird inconsistent indentation, then it won't track that or give you any way to preserve it. Whether this is a bug or a feature is debatable ;)

API

parse(txt, reviver = null, context = 20)

Works just like JSON.parse, but will include a bit more information when an error happens, and attaches a Symbol.for('indent') and Symbol.for('newline') on objects and arrays. This throws a JSONParseError.

parse.noExceptions(txt, reviver = null)

Works just like JSON.parse, but will return undefined rather than throwing an error.

class JSONParseError(er, text, context = 20, caller = null)

Extends the JavaScript SyntaxError class to parse the message and provide better metadata.

Pass in the error thrown by the built-in JSON.parse, and the text being parsed, and it'll parse out the bits needed to be helpful.

context defaults to 20.

Set a caller function to trim internal implementation details out of the stack trace. When calling parseJson, this is set to the parseJson function. If not set, then the constructor defaults to itself, so the stack trace will point to the spot where you call new JSONParseError.

版本与发布记录

json-parse-even-better-errors 共发布 12 个版本,首次发布于 2019/09/28;过去 12 个月发布了 1 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
6.0.0latest2026/05/095 个月前
5.0.02025/10/221 年前
4.0.02024/09/052 年前
3.0.22024/05/042 年前
3.0.12023/11/282 年前
3.0.02022/10/114 年前

关于 json-parse-even-better-errors 的常见问题

json-parse-even-better-errors 是什么?

JSON.parse with context information on error

如何安装 json-parse-even-better-errors?

在项目目录运行 npm install json-parse-even-better-errors,或使用 pnpm add / yarn add / bun add。

json-parse-even-better-errors 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/json-parse-even-better-errors 是否存在。

json-parse-even-better-errors 支持 ESM 还是 CommonJS?

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

json-parse-even-better-errors 需要什么版本的 Node.js?

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

json-parse-even-better-errors 可以商用吗?

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

json-parse-even-better-errors 的下载量有多少?

根据 npm 官方下载统计,json-parse-even-better-errors 在 2026/10/05 – 2026/11/03 这 30 天内被下载 457,991,348 次,日均约 15,266,378 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

json-parse-even-better-errors 还在维护吗?

最新版本 6.0.0 发布于 2026/05/09(5 个月前);至今共发布 12 个版本,其中过去 12 个月发布了 1 个;首次发布于 2019/09/28。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。