Skip to content

new.target undefined in ctor of CJS-nested class X extends Error — bundled-zod ZodError construction throws (config-schema error path) #6535

Description

@proggeramlug

Summary

new.target is undefined inside the constructor of a class X extends Error declared inside a CJS module function (webpack-wrapper shape). Reading new.target.prototype throws.

This is the residual error-path blocker behind #6530: with the #6530 family fixed, Next.js's config-schema.js initializes and configSchema.safeParse(<valid config>) matches node — but any validation failure constructs the bundled zod's ZodError, whose constructor does const t = new.target.prototypeTypeError: Cannot read properties of undefined (reading 'prototype') (in the reduced repro below the message is undefined is not a constructor).

Repro

nt_err.cjs:

(() => {
  "use strict";
  var mods = {
    7: function (e, t) {
      class MyErr extends Error {
        constructor(issues) {
          super();
          this.issues = [];
          const p = new.target.prototype;
          if (Object.setPrototypeOf) Object.setPrototypeOf(this, p);
          this.name = "MyErr";
          this.issues = issues;
        }
      }
      t.MyErr = MyErr;
    },
  };
  var cache = {};
  function req(id) {
    if (cache[id]) return cache[id].exports;
    var m = (cache[id] = { exports: {} });
    mods[id].call(m.exports, m, m.exports);
    return m.exports;
  }
  module.exports = req(7);
})();

nt_err.ts:

const m = require("./nt_err.cjs");
const e = new m.MyErr([{ code: "x" }]);
console.log("name:", e.name, "issues:", JSON.stringify(e.issues));

node: name: MyErr issues: [{"code":"x"}]
perry: TypeError: undefined is not a constructor

Notes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions