1

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

5
  • Please edit your question and add the full code. Commented Dec 15, 2023 at 13:27
  • What's your node version? fetch required node 18 stackoverflow.com/questions/48433783/… Commented Dec 15, 2023 at 17:21
  • I am using Node 16. However, I installed node-fetch, which still resulted in the same error. I don't see anywhere in Google's documentation that I'd need node 18 in order to use @google/generative-ai Commented Dec 16, 2023 at 20:41
  • If you're using a version of Node prior to 18, the fetch API is not implemented out-of-the-box and you'll need to use an external module for that, like node-fetch. See this ReferenceError: fetch is not defined Commented Feb 16, 2024 at 4:43
  • Does this answer your question? ReferenceError: fetch is not defined Commented Feb 16, 2024 at 4:45

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

this was my issue, I was using node version 16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.