0

I recently picked up a VSCode extension project I've done about a year ago.

In code it uses fetch to call APIs

enter image description here

This fetch used to work if I hit Start Debugging on this extension project, but now it doesn't.

Everytime it gets to this line, VSCode would complain fetch is not defined

enter image description here

There was literally zero change.

I did look at Package standard - "fetch" is not defined but changing to window.fetch would give window is not defined.

Any idea what's going on?

Thanks!

wxh
  • 619
  • 7
  • 20
  • 1
    have you tried `const fetch = require('node-fetch');`, install `node-fetch` with `npm` – rioV8 Oct 03 '22 at 22:36
  • global `fetch` is supported as of node 18.0.0. See https://nodejs.org/en/blog/announcements/v18-release-announce/#fetch-experimental Were you using the experimental fetch flag before. – Mark Oct 03 '22 at 23:05
  • And very likely you need to use `node-fetch` 2.x, https://github.com/vscode-restructuredtext/vscode-restructuredtext/blob/190.1.4/package.json#L17 – Lex Li Oct 04 '22 at 06:45
  • Yeah, both node-fetch and axios work. It's just weird I didn't have to install any of these. – wxh Oct 04 '22 at 23:10

2 Answers2

1

VS Code extensions run in node.js and not in the browser context so window.fetch is not available.

As of today VS Code ships with node.js 16.x, which does not have support for fetch yet. fetch should be supported natively as soon as VS Code updates its node.js version to any version >= 18.x.

Until then consider using the node-fetch@2 npm module.

Fabian Jakobs
  • 28,815
  • 8
  • 42
  • 39
-2

make the code block a separate function and then try again

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 07 '22 at 06:57