clean-stack

中文文档
7.0.0发布于 2026/11/03MITTypeScriptES Module近 30 天下载 1.8亿

Clean up error stack traces

原始使用文档
文档目录5 节
  1. Install
  2. Usage
  3. API
  4. cleanStack(stack, options?)
  5. Related

clean-stack

Clean up error stack traces

Removes the mostly unhelpful internal Node.js entries.

Also works in the browser and Electron.

Install

npm install clean-stack

Usage

import cleanStack from 'clean-stack';

const error = new Error('Missing unicorn');

console.log(error.stack);
/*
Error: Missing unicorn
    at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
    at Module._compile (module.js:409:26)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:139:18)
*/

console.log(cleanStack(error.stack));
/*
Error: Missing unicorn
    at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
*/

API

cleanStack(stack, options?)

Returns the cleaned stack or undefined if the given stack is not a string.

stack

Type: string | undefined

The stack property of an Error, or undefined.

options

Type: object

pretty

Type: boolean
Default: false

Prettify the file paths in the stack:

  • /Users/sindresorhus/dev/clean-stack/unicorn.js:2:15 → ~/dev/clean-stack/unicorn.js:2:15
  • file:///Users/sindresorhus/dev/clean-stack/unicorn.js:2:15 → ~/dev/clean-stack/unicorn.js:2:15

When enabled, file URLs are converted to regular paths for better readability and IDE integration. URLs that cannot be converted, contain ? or #, or decode to a line break are left unchanged.

pretty and basePath only rewrite frame paths. Backslashes become / throughout each frame. Error message text is preserved except for blank lines; lines shaped like frames are treated as frames.

basePath

Type: string?

Remove the given base path from stack trace file paths, effectively turning absolute paths into relative ones. It will also transform absolute file URLs into relative paths.

Supports absolute or relative paths, either separator, and the filesystem root. A leading slash only matches absolute paths. Relative base paths do not match inside file:///, except Windows drive paths such as C:/Users.

Example with '/Users/sindresorhus/dev/clean-stack' as basePath:

/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15 → unicorn.js:2:15

[!NOTE] basePath matches before file URLs are decoded, so /Users/John Doe/dev does not match file:///Users/John%20Doe/dev. After a match, remaining URL escapes stay encoded, even with pretty.

[!NOTE] Frames like at fn (unicorn.js:2:15) are removed because they resemble old internal Node.js frames. Cleaning again after basePath produces this form can therefore drop frames for files directly in the base path. Paths with a directory, like at fn (src/unicorn.js:2:15), and bare frames like at unicorn.js:2:15 are unaffected.

pathFilter

Type: (path: string) => boolean

Remove stack lines where the callback returns false. It receives the path without the closing parenthesis, with / separators, before basePath or pretty is applied. A pathless frame such as at async Promise.all (index 0) yields index 0.

[!NOTE] Balanced parentheses in function names, such as at render (partial) (/base/a.js:6:9), and paths, such as C:/Program Files (x86)/app.js:1:1, are supported. Unbalanced parentheses in a function name, such as at a(b (/base/a.js:6:9), can cause part of the name to be included in the path.

import cleanStack from 'clean-stack';

const error = new Error('Missing unicorn');

console.log(cleanStack(error.stack));
// Error: Missing unicorn
//     at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)
//     at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/omit-me.js:1:16)

const pathFilter = path => !/omit-me/.test(path);

console.log(cleanStack(error.stack, {pathFilter}));
// Error: Missing unicorn
//     at Object.<anonymous> (/Users/sindresorhus/dev/clean-stack/unicorn.js:2:15)

版本与发布记录

clean-stack 共发布 23 个版本,首次发布于 2016/07/08;过去 12 个月发布了 1 个版本。

版本发布日期距今
7.0.0latest2026/11/033 天前
6.0.02025/11/041 年前
5.3.02025/09/161.1 年前
5.2.02023/03/213 年前
5.1.02022/12/213 年前
5.0.12022/10/274 年前

关于 clean-stack 的常见问题

clean-stack 是什么?

Clean up error stack traces

如何安装 clean-stack?

在项目目录运行 npm install clean-stack,或使用 pnpm add / yarn add / bun add。

clean-stack 支持 TypeScript 吗?

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

clean-stack 支持 ESM 还是 CommonJS?

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

clean-stack 需要什么版本的 Node.js?

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

clean-stack 可以商用吗?

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

clean-stack 的下载量有多少?

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

clean-stack 还在维护吗?

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