node
runtime_error
ai_generated
true
ReferenceError: fetch is not defined
ID: node/fetch-not-defined
95%Fix Rate
95%Confidence
50Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 18 | active | — | — | — |
Root Cause
fetch() is experimental in Node 18, stable in Node 21+. Common when porting browser code.
genericWorkarounds
-
95% success Install node-fetch: npm install node-fetch
import fetch from 'node-fetch';
-
90% success Upgrade to Node 21+ where fetch is stable, or use --experimental-fetch flag in Node 18
nvm install 22 && nvm use 22 # Or for Node 18: node --experimental-fetch app.js
-
88% success Use undici (Node's built-in HTTP client): const { fetch } = require('undici')
const { fetch } = require('undici')
Dead Ends
Common approaches that don't work:
-
Polyfill with whatwg-fetch
80% fail
whatwg-fetch is browser-only, doesn't work in Node
-
Use XMLHttpRequest
90% fail
XMLHttpRequest doesn't exist in Node
Error Chain
Frequently confused with: