5

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.

pu nanjo
  • 51
  • 1
  • 2
  • 1
    Please check [this](https://github.com/node-fetch/node-fetch/issues/1416#issuecomment-994109019) – jagad89 Mar 22 '22 at 03:52

2 Answers2

2

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);

user12452512
  • 71
  • 1
  • 2
-4

Nestjs provide HTTP Module. Here is the doc url: https://docs.nestjs.com/techniques/http-module