google-gemini / generative-ai-js

The official Node.js / Typescript library for the Google Gemini API
https://ai.google.dev/
Apache License 2.0
502 stars 96 forks source link

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

Open Binary-Bytes opened 1 week ago

Binary-Bytes commented 1 week ago

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

hsubox76 commented 4 days ago

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?