Skip to content

Allow using --enable-source-maps together with custom Error.prepareStackTrace #50733

Closed
@nicolo-ribaudo

Description

@nicolo-ribaudo

What is the problem this feature will solve?

Some libraries use Error.prepareStackTrace to make their errors more user-friendly. One example is Babel: when we throw an error due to user configs, we hide most of our internal stack frames to that our users can more easily find where they originally called Babel.

To make sure that our implementation works well together with other libraries that use Error.prepareStackTrace, we do something like this:

const { prepareStackTrace = defaultPrepareStackTrace } = Error;

Error.prepareStackTrace = function (error, frames) {
  updateTheFrames(frames);
  // Call the original stack trace formatter
  return prepareStackTrace(error, frames);
};

As the default defaultPrepareStackTrace we use the following, which matches Node.js' default behavior:

function defaultPrepareStackTrace(err: Error, trace: CallSite[]) {
  if (trace.length === 0) return ErrorToString(err);
  return `${ErrorToString(err)}\n    at ${trace.join("\n    at ")}`;
}

I am currently using Babel together with the source-map-support package, so that errors show their original source files in the stack traces rather than the compiled ones. This works well, because source-map-support simply installs a custom Error.prepareStackTrace that then Babel's Error.prepareStackTrace will use to format the error.

A couple days ago @joyeecheung introduced me to Node.js's --enable-source-maps flag (https://nodejs.org/api/cli.html#--enable-source-maps), which basically replaces the source-map-support package. I tried to use it, but I cannot because I'm using defaultPrepareStackTrace that mimics Node.js' built-in behavior, rather than it being what Node.js does.

What is the feature you are proposing to solve the problem?

Node.js should expose its default error formatter, maybe as require("util").prepareStackTrace or as a pre-defined Error.prepareStackTrace function.

This would allow some chaining similar to how ESM loaders can compose.

What alternatives have you considered?

I'm very open to any other solution that would allow me to write an Error.prepareStackTrace that works well together with --enable-source-maps.

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature requestIssues that request new features to be added to Node.js.source mapsIssues and PRs related to source map support.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions