ReferenceError: fetch is not defined (my node version is >18)
Description of the bug:
I am building an extension for Raycast, while using the Gemini API, however getting the following error when I am just following the Quickstart guide. I saw the previous same issues but my node version is already more than 18.
ReferenceError: fetch is not defined
makeModelRequest:index.mjs:309:10
---
306: }
307: async function makeModelRequest(model, task, apiKey, stream, body, requestOptions,
308: // Allows this to be stubbed for tests
309: fetchFn = fetch) {
310: const { url, fetchOptions } = await constructModelRequest(model, task, apiKey, stream, body, requestOptions);
311: return makeRequest(url, fetchOptions, fetchFn);
312: }
---
generateContent:index.js:509:26
GenerativeModel.generateContent:index.js:811:12
run:index.js:892:30
DEVICE MacOS Sonoma Node v20.14.0 npm 10.7.0
Sounds similar to https://github.com/google-gemini/generative-ai-js/issues/173. Can you answer these questions?
This error will occur if there's no
fetchavailable in the global namespace. If you're using Node 20, this shouldn't be a problem. Are you able to run a simple node script in that same environment that just callsfetch()(on any valid url)? Are you using any kind of framework or library that polyfills or overwritesfetch?
I get the same error, but after a small pause of interaction with the model. I am running node 20.18.1
@bcatalin just in case, is it possible for you to have multiple node versions that are causing you CLI to give you one value while your application is running in other?
And do you know how much time w/o interaction with the model elapses before fetch cannot be found?
@IvanLH I have only one version of nodejs on my instance and the time without interaction was around 30 seconds or so.
@bcatalin I tried reproing with a simple node application which listened to text output and called generateContent() off of it. I could not, using the same version of node as you.
Some possibilities that occur to me, is your application redefining fetch somewhere, or uses a library such as cross-fetch that does it for you?
And are you running your node application in a special way? Some startup flags that are not the default ones that could help me repro?
Hi @IvanLH , thanks for checking in. I've created a new instance and now everything is working correctly. Maybe it was a glitch in the Matrix :-)
Thanks for support.
I was having this issue on my old node backend, I updated everything and still had the error, turns out I'm an idiot and was forcing the system to use my old node version in the batch that started the service. Now it's working folks, sorry