Description
Pure ESM package
node-fetch is an ESM-only module - you are not able to import it with require()
. You don't necessary have to convert your hole project to ESM just b/c we did it. There are ways to load ESM modules in cjs package too (more about this in FAQ). You can also stay with the v2 branch - We will keep updating v2 with bug/security issues. But not so much with new features...
This means you have the following choices:
- Use ESM yourself. (preferred)
Useimport fetch from 'node-fetch'
instead ofconst fetch = require('node-fetch')
to import the package. You also need to put"type": "module"
in your package.json and more. Follow the below guide. - you could use async
import(…)
from CommonJS instead ofrequire(…)
. This is supported in CommonJS also - Stay on the existing version of the package until you can move to ESM.
You also need to make sure you're on the latest minor version of Node.js. At minimum Node.js 12.20, 14.14, or 16.0.
I would strongly recommend moving to ESM. ESM can still import CommonJS packages, but CommonJS packages cannot import ESM packages synchronously.
ESM is natively supported by Node.js 12.17 and later.
FAQ
- How can I move my CommonJS project to ESM?
- Can I import ESM packages in my CJS project?
- How do I get typing support if I'm using async import?
- Can I import ESM packages in my TypeScript project?
- How can I make my TypeScript project output ESM?
- I'm having problems with ESM and TypeScript
- TypeScript/WebPack rewrites my async imports to require in CJS projects
- I'm having problems with ESM and
ts-node
- Can I run a ESM file/project without a package.json?
Have questions about another tools? look trough other issues or ask new ones
We will continue to update this with new solutions from other accepted answers