or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-clean-stack

Clean up error stack traces by removing unhelpful internal Node.js entries

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/clean-stack@5.2.x

To install, run

npx @tessl/cli install tessl/npm-clean-stack@5.2.0

index.mddocs/

Clean Stack

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.

Package Information

  • Package Name: clean-stack
  • Package Type: npm
  • Language: JavaScript (ES modules)
  • Installation: npm install clean-stack
  • Dependencies: escape-string-regexp

Core Imports

import cleanStack from "clean-stack";

For CommonJS:

const cleanStack = require("clean-stack");

Architecture

Clean Stack is designed with platform-specific optimizations:

  • Core Module (index.js): Main stack cleaning logic with regex-based filtering
  • Home Directory Helpers: Platform-specific modules for path prettification
    • home-directory.js: Node.js implementation using os.homedir()
    • home-directory-browser.js: Browser implementation (returns empty string)
  • Conditional Imports: Uses package.json imports field for automatic platform selection
  • Regex Engine: Uses escape-string-regexp for safe regex pattern generation

Basic 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)
*/

Capabilities

Stack Trace Cleaning

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.js

With 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:15

With 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)
});

Types

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;
}

Platform Support

Clean Stack works in both Node.js and browser environments:

  • Node.js: Full functionality including home directory prettification
  • Browser: Full functionality with empty home directory (no ~ expansion)
  • Electron: Special handling for Electron-specific stack trace noise