generative-ai-js icon indicating copy to clipboard operation
generative-ai-js copied to clipboard

ReferenceError: fetch is not defined (my node version is >18)

Open Binary-Bytes opened this issue 1 year ago • 7 comments

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

Binary-Bytes avatar Jun 22 '24 08:06 Binary-Bytes

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 fetch available 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 calls fetch() (on any valid url)? Are you using any kind of framework or library that polyfills or overwrites fetch?

hsubox76 avatar Jun 24 '24 18:06 hsubox76

I get the same error, but after a small pause of interaction with the model. I am running node 20.18.1

bcatalin avatar Feb 05 '25 20:02 bcatalin

@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 avatar Mar 04 '25 23:03 IvanLH

@IvanLH I have only one version of nodejs on my instance and the time without interaction was around 30 seconds or so.

bcatalin avatar Mar 05 '25 08:03 bcatalin

@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?

IvanLH avatar Mar 06 '25 00:03 IvanLH

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.

bcatalin avatar Mar 06 '25 18:03 bcatalin

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

bamtang-dev avatar Mar 10 '25 16:03 bamtang-dev