package
Clean up error stack traces
npm install clean-stackGitHub-flavored package documentation.
Across all versions, last 30 days.
230,883,218 in 30 days
Runtime metadata.
Tagged topics.
Clean up error stack traces
Removes the mostly unhelpful internal Node.js entries.
Also works in the browser and Electron.
npm install clean-stackimport 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)
*/Returns the cleaned stack or undefined if the given stack is not a string.
Type: string | undefined
The stack property of an Error, or undefined.
Type: object
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:15file:///Users/sindresorhus/dev/clean-stack/unicorn.js:2:15 → ~/dev/clean-stack/unicorn.js:2:15When 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.
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]
basePathmatches before file URLs are decoded, so/Users/John Doe/devdoes not matchfile:///Users/John%20Doe/dev. After a match, remaining URL escapes stay encoded, even withpretty.
[!NOTE] Frames like
at fn (unicorn.js:2:15)are removed because they resemble old internal Node.js frames. Cleaning again afterbasePathproduces this form can therefore drop frames for files directly in the base path. Paths with a directory, likeat fn (src/unicorn.js:2:15), and bare frames likeat unicorn.js:2:15are unaffected.
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 asC:/Program Files (x86)/app.js:1:1, are supported. Unbalanced parentheses in a function name, such asat 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)Estimated unpacked size across 3 packages.
34.4 kB
Includes dependencies.
^5.0.0
5.0.0