Clean Stack provides utilities for cleaning up JavaScript error stack traces by removing unhelpful internal Node.js entries and Electron-specific noise. It transforms verbose, cluttered stack traces into clean, readable ones that focus on your application code.
npm install clean-stackimport cleanStack from "clean-stack";For CommonJS:
const cleanStack = require("clean-stack");Clean Stack is designed with platform-specific optimizations:
index.js): Main stack cleaning logic with regex-based filteringhome-directory.js: Node.js implementation using os.homedir()home-directory-browser.js: Browser implementation (returns empty string)imports field for automatic platform selectionescape-string-regexp for safe regex pattern generationimport 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)
*/Cleans error stack traces by removing unhelpful internal Node.js entries and Electron-specific noise.
/**
* Clean up error stack traces
* @param stack - The stack property of an Error object
* @param options - Configuration options for cleaning behavior
* @returns The cleaned stack trace or undefined if input stack is undefined
*/
function cleanStack(stack: string | undefined, options?: Options): string | undefined;
interface Options {
/** Prettify file paths by replacing home directory with ~ */
readonly pretty?: boolean;
/** Base path to remove from file paths, making them relative */
readonly basePath?: string;
/** Custom function to filter stack lines based on file path */
readonly pathFilter?: (path: string) => boolean;
}Usage Examples:
Basic cleaning:
import cleanStack from "clean-stack";
const error = new Error("Something went wrong");
const cleaned = cleanStack(error.stack);With prettification:
const cleaned = cleanStack(error.stack, { pretty: true });
// Transforms: /Users/sindresorhus/dev/project/file.js
// To: ~/dev/project/file.jsWith base path removal:
const cleaned = cleanStack(error.stack, {
basePath: "/Users/sindresorhus/dev/clean-stack"
});
// Transforms: /Users/sindresorhus/dev/clean-stack/unicorn.js:2:15
// To: unicorn.js:2:15With custom path filtering:
const pathFilter = path => !/node_modules/.test(path);
const cleaned = cleanStack(error.stack, { pathFilter });
// Filters out any stack lines containing 'node_modules'Combined options:
const cleaned = cleanStack(error.stack, {
pretty: true,
basePath: "/Users/sindresorhus/dev/project",
pathFilter: path => !/test-utils/.test(path)
});interface Options {
/**
* Prettify the file paths in the stack by replacing home directory with ~
* Default: false
*/
readonly pretty?: boolean;
/**
* Remove the given base path from stack trace file paths,
* effectively turning absolute paths into relative ones
*/
readonly basePath?: string;
/**
* Remove the stack lines where the given function returns false.
* The function receives the path part of the stack line.
*/
readonly pathFilter?: (path: string) => boolean;
}Clean Stack works in both Node.js and browser environments: