- Published on
Fix "fetch is not defined" error in external packages
- Ashik Nesin
If you're on older versions of Node.js, then chances are you might be running into an error when you try to use fetch natively.
"ReferenceError: fetch is not defined" for external packages
The fix is straight forward. Use node-fetch
npm install node-fetch
fetch to global, it will resolve the issue even for external packages
Polyfill for ESM
import fetch from 'node-fetch'; global.fetch = fetch;
Polyfill for CommonJS
const fetch = require('node-fetch'); global.fetch = fetch;
Happy fixing fetch!