Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Vercel function crashes with Cannot find module under /var/task/.../node_modules

ErrorError: Cannot find module '/var/task/backend/node_modules/axios/dist/node/axios.cjs'

Short answer

The function was deployed without that file in its bundle. Vercel installs dependencies from the package.json it is pointed at, usually the project root, and bundles only files it can trace. Install from the folder that holds the package, or add the missing files to the function with includeFiles.

  • Applies to Node.js 22, Vercel, axios 1.x
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find where the package.json that lists the package lives, and compare it with where Vercel installs. The folder in the path (backend here) is your hint.

    ls package.json backend/package.json
    grep -n "axios" package.json backend/package.json
  2. 2Match what you see, then jump to its fix

    1. 1The missing path starts with a folder such as backend and your root package.json has no such dependencyDependencies live in a subfolder that Vercel does not installFix
    2. 2The package installs fine, but one file inside it is missing at runtimeThe file exists but was not included in the function bundleFix

What does the error look like?

  • Error: Cannot find module '/var/task/backend/node_modules/axios/dist/node/axios.cjs'

Cause 1 of 2

Dependencies live in a subfolder that Vercel does not install

1Check

For Node.js, Vercel installs from the package.json at the root of the project, and the Root Directory setting decides that path. A package.json in a subfolder is not installed unless you tell Vercel to.

cat vercel.json
ls backend

2Fix

Either set Root Directory to the subfolder that has the package.json, or give the functions an install command that installs from there.

{
  "functions": {
    "api/**/*.js": {
      "installCommand": "npm install --prefix backend"
    }
  }
}

3Verify

Redeploy and call the endpoint. The log shows no Cannot find module error.

curl -i https://your-app.vercel.app/api/health

Confirmed by vercel.com

Did this fix it?

Cause 2 of 2

The file exists but was not included in the function bundle

1Check

Vercel bundles the files it can trace. A file loaded in a way the tracer does not follow is left out. includeFiles is the documented way to add files to a function.

ls backend/node_modules/axios/dist/node/

2Fix

Add the files with a glob in vercel.json, matching your function's path.

{
  "functions": {
    "api/index.js": {
      "includeFiles": "backend/node_modules/axios/dist/node/**"
    }
  }
}

3Verify

Redeploy and request the route again.

curl -i https://your-app.vercel.app/api/health

Confirmed by vercel.com

Did this fix it?

How do I stop it coming back?

  • Keep one package.json per deployed project, at its root directory.
  • Set Root Directory in Project Settings instead of nesting the app in the repository root.
  • Call the deployed route once after every deploy.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.