Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node-fetch@3 Not support in nestjs because it is ESM

I can't use the package "node-fetch@3". I just import it and my console will log the error:

const node_fetch_1 = require("node-fetch");
                     ^
Error [ERR_REQUIRE_ESM]: require() of ES Module /home/control-panel-nest2/node_modules/node-fetch/src/index.js from /home/control-panel-nest2/dist/bimwize/document.service.js not supported.
Instead change the require of index.js in /home/control-panel-nest2/dist/bimwize/document.service.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/control-panel-nest2/dist/bimwize/document.service.js:19:22)
    at Object.<anonymous> (/home/control-panel-nest2/dist/bimwize/bimwize.module.js:14:28)
    at Object.<anonymous> (/home/control-panel-nest2/dist/user/user.module.js:11:26)
    at Object.<anonymous> (/home/control-panel-nest2/dist/app.module.js:17:23)
    at Object.<anonymous> (/home/control-panel-nest2/dist/main.js:6:22)

I know what happend. node-fetch@3 only support esm but not commonjs and nestjs will compile all my import to require.

I don't know how to fix this problem. I have try to use node-fetch@2, but node-fetch@2 missing some of the feature I wanted.

like image 668
pu nanjo Avatar asked Oct 22 '25 05:10

pu nanjo


1 Answers

source: https://github.com/node-fetch/node-fetch

  • npm install node-fetch@2

import fetch from 'node-fetch';

const response = await fetch('https://api.github.com/users/github'); const data = await response.json();

console.log(data);

like image 170
user12452512 Avatar answered Oct 25 '25 15:10

user12452512



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!