0

I googled for a while but I could not find a solution that fixed my issue.

I want to use the fetch function from "node-fetch", but when requiring it in my code, I get the error:

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/xvma/stuff/Projekte/React/first-try/node_modules/node-fetch/src/index.js
require() of ES modules is not supported.
require() of /Users/xvma/stuff/Projekte/React/first-try/node_modules/node-fetch/src/index.js from /Users/xvma/stuff/Projekte/React/first-try/test.js is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/xvma/stuff/Projekte/React/first-try/node_modules/node-fetch/package.json.

Terminal output

In this file I want to use fetch()

My server.js

I hope you guys can help me, I tried deleting my node-components folder & package lock but that did not work. Also, in my package.lock.json "type"="module" is not set.

xvma
  • 1
  • 1

1 Answers1

0

It looks like you're using node-fetch 3, which is only distributed as an ESM module, not require()able CJS modules.

  • For the time being, you can just downgrade to a node-fetch less than version 3.
  • Alternately, you can go all in on ESM and rename your script to server.mjs and use import everywhere.
AKX
  • 152,115
  • 15
  • 115
  • 172