"Cannot find module" for @google/genai for serverless function

Hello!

I’m facing a very unusual runtime error and I’m hoping for some expert help.

The Problem (TL;DR):

My Node.js project deploys successfully to Vercel. However, when I call any API endpoint that uses the @google/genai package, the function crashes with a 500 - FUNCTION_INVOCATION_FAILED error.

The runtime log for that specific function call shows the following error:
Cannot find module '/var/task/node_modules/@google/genai/dist/node/index.cjs'

This indicates that while the build process seems to work, the @google/genai package is not present in the final serverless function artifact at runtime.

What I Have Already Tried:

I have performed extensive troubleshooting to resolve this, including:

  1. Confirmed package.json: @google/genai is correctly listed in "dependencies".
  2. Created a Fresh Project: The error persists even on a brand-new, minimal project linked to a new GitHub repo to rule out hidden configuration issues.
  3. “Clean Slate” Reset: I have deleted node_modules and lock files, reinstalled, and pushed the new lock file.
  4. Cleared Vercel’s Build Cache: I have redeployed multiple times with the build cache disabled.
  5. Verified API Key: My GEMINI_API_KEY is correct and works perfectly in direct cURL requests to the Google API.

My Core Question:

Why would a dependency that appears to be correctly installed during the build not be available at runtime? I am using standard import statements, not dynamic ones.

This seems like a platform-level issue with the build output. Any insights or suggestions would be greatly appreciated.

Thank you!

This looks like a bundling issue with the @google/genai package in Vercel’s serverless functions. I asked v0 for some suggestions:

1. Check your import statement
Make sure you’re importing the package correctly:

import { GoogleGenerativeAI } from '@google/genai'

2. Add to vercel.json functions config
Try explicitly including the package in your function configuration:

{
  "functions": {
    "app/api/**/*.js": {
      "includeFiles": "node_modules/@google/genai/**"
    }
  }
}

3. Check Node.js runtime version
Ensure you’re using a supported Node.js version (18.x or 20.x) in your package.json:

{
  "engines": {
    "node": "20.x"
  }
}

If none of these work, could you share your API route function and package.json? That would help us see if there’s something specific about how you’re using the package.