Error Stack Parser
stack-parser is a minimalist utility for parsing JavaScript error stack traces within Node.js environments. It takes a raw error stack string and converts it into a structured array of objects, each representing a stack frame. The package provides methods to format these frames, including custom formatting options. As of 2026, its stable version remains `0.0.1`, initially released in 2012. This indicates the package is effectively unmaintained and has seen no significant updates or feature additions over a decade. Due to its age, it primarily targets older Node.js versions and CommonJS environments, lacking support for modern ES Modules or TypeScript definitions. While functional for basic stack parsing, developers should be aware of its abandonment and consider more actively maintained alternatives for robust, cross-environment stack parsing needs.
Common errors
-
ReferenceError: require is not defined
cause Attempting to use `require('stack-parser')` in an ES Module (ESM) context without proper transpilation or dynamic import.fixEnsure your file is a CommonJS module (e.g., by using `.cjs` extension or setting `"type": "commonjs"` in package.json) or use dynamic import `import('stack-parser').then(module => { const stackParser = module.default || module; /* ... */ });`. -
Error: Cannot find module 'stack-parser'
cause The `stack-parser` package has not been installed or is not resolvable in the current Node.js environment.fixRun `npm install stack-parser` or `yarn add stack-parser` in your project directory. -
Property 'parse' does not exist on type 'typeof import("/path/to/node_modules/stack-parser/index")'.cause TypeScript compilation error due to lack of type definitions for the `stack-parser` package.fixCreate a `declarations.d.ts` file with `declare module 'stack-parser';` or explicitly cast the import: `const stackParser: any = require('stack-parser');`.
Warnings
- deprecated This package is effectively abandoned, with its last release (v0.0.1) dating back to 2012. It is not maintained and may not be compatible with modern Node.js features or JavaScript syntax.
- gotcha The package is a CommonJS module and does not provide an ES Module (ESM) export. Attempting to `import` it directly in an ESM context will result in a runtime error.
- gotcha There are no TypeScript type definitions available for this package. Using it in a TypeScript project will result in type errors unless explicit `any` types are used or custom declaration files are created.
- gotcha The parser's robustness against different JavaScript engine stack trace formats (e.g., V8, SpiderMonkey, Chakra) or varying Node.js versions is unknown and likely limited given its age and lack of updates. Modern environments might produce stack formats it cannot fully parse.
Install
-
npm install stack-parser -
yarn add stack-parser -
pnpm add stack-parser
Imports
- stackParser
import stackParser from 'stack-parser';
const stackParser = require('stack-parser'); - parse
import { parse } from 'stack-parser';const stackParser = require('stack-parser'); const items = stackParser.parse((new Error).stack); - here
const stackParser = require('stack-parser'); const items = stackParser.here();
Quickstart
const stackParser = require('stack-parser');
// Parse the current error stack
const items = stackParser.parse((new Error).stack);
console.log('--- Default Formatting ---');
items.forEach(function(item) {
console.log(item.format());
});
console.log('\n--- Custom Formatting (long) ---');
items.forEach(function(item) {
console.log(item.format('%what (%file, line: %line, column: %column)'));
});
console.log('\n--- Custom Formatting (short) ---');
items.forEach(function(item) {
console.log(item.format('%w (%f, line: %l, column: %c)'));
});
console.log('\n--- Using .here() for current stack ---');
const currentStackItems = stackParser.here();
currentStackItems.forEach(function(item) {
console.log(item.format());
});