Google's Gemini for Nodejs results in error ReferenceError: fetch is not defined

465 views Asked by At

I am trying Google's Gemini model in Node js as suggested in their documentation here

However, when I executed their sample code in my node application, i receive the following error:

ReferenceError: fetch is not defined

This is the code block i'm trying:

const model = genAI.getGenerativeModel({ model: "gemini-pro"});
const prompt = "Write a story about a magic backpack."
const result = await model.generateContent(prompt);
const response = await result.response;
const text = response.text();
console.log(text);

Anyone know how the issue?

Followed exactly what the documentation example

1

There are 1 answers

1
andibastian On

you have to use node version minimum 18 or later. it's work