Error Polyfill for V8 Stack Trace API
The `error-polyfill` package provides a polyfill for the V8 Stack Trace API, enabling functionalities like `Error.captureStackTrace` and `Error.prepareStackTrace` in non-V8 JavaScript environments. It aims to standardize how stack traces are generated and accessed across various engines and browsers, including older versions of Node.js, Chrome, Firefox, Internet Explorer, and PhantomJS. The current stable version is 0.1.3, suggesting an early stage of development and a lack of recent updates. A key differentiator is the `Error.getStackTrace` utility, which is recommended for reliably accessing formatted stack traces, as direct `throwable.stack` access might be inconsistent or unformatted in polyfilled environments. The library explicitly requires ES5 support to function.
Common errors
-
Error: ES5 support is required!
cause The runtime environment where the polyfill is executed lacks full ES5 feature support, which is a hard requirement for the library.fixEnsure your execution environment (browser, Node.js version) provides complete ES5 support. For very old or non-compliant environments, pre-process your code with a transpiler (e.g., Babel) to ensure ES5 compatibility. -
TypeError: Error.captureStackTrace is not a function
cause The `error-polyfill` library, which adds `captureStackTrace` as a static method to the global `Error` object, was not loaded or executed before code attempted to call `Error.captureStackTrace`.fixEnsure `require("error-polyfill");` is placed at the very top of your application's main entry file or explicitly before any code that attempts to use the polyfilled `Error` methods.
Warnings
- gotcha The `error-polyfill` library explicitly requires ES5 support to function. Running in environments that do not fully support ES5 will cause the library to throw an error and stop working.
- gotcha When `error-polyfill` is active, especially in non-V8 environments, directly accessing `throwable.stack` may yield unformatted or inconsistent stack trace strings. The polyfill mechanism involves parsing and reformatting the stack.
- breaking This package is effectively abandoned, indicated by its very low version (0.1.3), reliance on outdated compatibility targets (Node.js 8/9, PhantomJS 2.1, old browser versions), and use of an inactive CI platform (Travis CI). It is unlikely to function correctly or integrate seamlessly with modern JavaScript runtimes (e.g., Node.js 16+), contemporary browser environments, or modern bundlers (Webpack 5+, Vite) without significant manual polyfilling, configuration, or potential conflicts.
Install
-
npm install error-polyfill -
yarn add error-polyfill -
pnpm add error-polyfill
Imports
- error-polyfill
import 'error-polyfill';
require("error-polyfill"); - Error.captureStackTrace
new Error().captureStackTrace(this, this.constructor);
Error.captureStackTrace(this, this.constructor);
- Error.getStackTrace
error.stack;
Error.getStackTrace(error);
Quickstart
require("error-polyfill");
// Define a custom error to demonstrate captureStackTrace
var CustomError = function (message) {
this.name = "CustomError";
this.message = message;
// Capture the current stack trace, excluding the CustomError constructor call
Error.captureStackTrace(this, this.constructor);
};
// Inherit from Error.prototype to ensure proper error instance behavior
CustomError.prototype = Object.create(Error.prototype);
CustomError.prototype.constructor = CustomError; // Maintain correct constructor reference
function doSomethingWithError() {
function triggerError() {
throw new CustomError("An intentional error occurred!");
}
triggerError();
}
try {
doSomethingWithError();
} catch (error) {
console.log("Caught an error:");
if (error instanceof CustomError) {
console.log(` Type: ${error.name}`);
console.log(` Message: ${error.message}`);
}
// Always use Error.getStackTrace for consistent results with this polyfill
console.log(" Stack Trace:\n" + Error.getStackTrace(error));
}
// Example of capturing a stack without throwing an error
const myObject = {};
Error.captureStackTrace(myObject);
console.log('\nCaptured stack for a plain object:\n' + Error.getStackTrace(myObject));