Search code examples
node.jstypescriptvue.jsexpressnodemon

Why did fetch stop working - for me - on all branches in backend server?


After a refactor, on a separate branch, fetch no longer works in any branch. Everything else works as expected. Error:

...server/KE/utils.ts:44
const response = await fetch(
                 ^
ReferenceError: fetch is not defined

Refactor was moving logic from KE/router.ts to a function in KE/utils.ts.
fetch is present in lib.dom.d.ts.
Had a friend a friend run the code and they were able to call fetch without issue.

Fixes attempted

  • I moved the function from KE/utils.ts logic back to KE/router.ts.
  • Copied all imports from KE/router to KE/utils.
  • Installed node-fetch. Import fetch from 'node-fetch'. Stopped compiling because as it messed with express's data types.
  • Checked previous branch which used to work.
  • Reinstalling all packages with npm.
  • Reinstalled npm with curl -qL https://www.npmjs.com/install.sh | sudo sh

Versions

  • npm - 9.6.2
  • express - 4.18.2
  • vue - 3.2.47
  • typescript - 4.5.5
  • nodemon - 2.0.21

Solution

  • Back-end runs on Node. Update node to a version >= 18.0.0 to fix. Source.

    To install latest node with nvm:

    nvm install node
    

    Credit: @Mike'Pomax'Kamermans