Yoshi
Yoshi

Reputation: 141

Cannot find module '@google/generative-ai'

I have the following js code called script.js in a local folder. This code is to call Gemini API.

const { GoogleGenerativeAI } = require("@google/generative-ai");

const generationConfig = {
  temperature: 0.7,
  candidateCount: 1,
  topK: 40,
  topP: 0.95,
  maxOutputTokens: 1024,
};

const safetySettings = [
  {
    category: 'HARM_CATEGORY_DANGEROUS_CONTENT',
    threshold: 'BLOCK_NONE'
  },
];

const genAI = new GoogleGenerativeAI(process.env.API_KEY);

const model = genAI.getGenerativeModel({
  model: "gemini-pro",
});

model.generateContent({
  generationConfig,
  safetySettings,
  contents: [
    {
      role: "user",
      parts: [
        { text: 'On what planet do humans live? ' }
      ]
    },
  ],
}).then(result => {
  console.log(JSON.stringify(result, null, 2));
});

I have the following code in my package.json, after running the command npm install @google-ai/generativelanguage .

 {
  "name": "googleai",
  "version": "1.0.0",
  "description": "",
  "main": "script.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@google-ai/generativelanguage": "^2.1.1"
  }
}

However, when I run node script.js, I get the following error.

yoshikiyamaguchi@YoshikinoMacBook-Pro GoogleAI % node script.js                                          
node:internal/modules/cjs/loader:1145
  throw err;
  ^

Error: Cannot find module '@google/generative-ai'
Require stack:
- /Users/yoshikiyamaguchi/GoogleAI/script.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1142:15)
    at Module._load (node:internal/modules/cjs/loader:983:27)
    at Module.require (node:internal/modules/cjs/loader:1230:19)
    at require (node:internal/modules/helpers:179:18)
    at Object.<anonymous> (/Users/yoshikiyamaguchi/GoogleAI/script.js:1:32)
    at Module._compile (node:internal/modules/cjs/loader:1368:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1426:10)
    at Module.load (node:internal/modules/cjs/loader:1205:32)
    at Module._load (node:internal/modules/cjs/loader:1021:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/yoshikiyamaguchi/GoogleAI/script.js' ]
}

I cannot figure out why it keeps saying module_not_found. What am I doing wrong?

Upvotes: 3

Views: 1788

Answers (2)

I faced a similar issue and managed to resolve it. The problem in my case was with the import statement. I was importing from @google/generative-AI with a capital “AI”, while the actual package name in package.json was in lowercase as @google/generative-ai.

Upvotes: 0

Priyansh jain
Priyansh jain

Reputation: 1422

I resolved this issue recently there can be 2 reasons for it:

  1. Either you are installing @google-cloud/generative-ai and importing @google/generative-ai. See the difference in -cloud after google. This was the reason for my error. Check if you are installing same package as you are importing.
  2. You might not have installed @google/generative-ai properly and removed the critical vulnerability. To do this, run npm install @google/generative-ai first and then run npm audit fix or npm audit fix --force

Upvotes: 0

Related Questions