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.
npm install error-polyfillVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to load the `error-polyfill`, define a custom error using the polyfilled `Error.captureStackTrace` for structured stack tracing, and then retrieve the formatted stack trace using the recommended `Error.getStackTrace` in both error handling and for general stack capture scenarios.
Ensure the JavaScript execution environment fully supports ES5. For older browsers, this might necessitate transpiling your code to ES5, though this polyfill targets older runtimes where native ES5 support was expected or could be shimmed separately.
Always use the static method `Error.getStackTrace(throwable)` provided by the polyfill to reliably retrieve properly formatted stack traces from `Error` (or `Object`) instances.
For new projects, consider using native `Error.stack` if your target environments support it, or opt for a more actively maintained polyfill or transpilation solution for consistent stack trace management. Avoid `error-polyfill` in environments that are not explicitly the deprecated ones it targets.
Ensure 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.
Ensure `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.