Vercel function crashes with Cannot find module under /var/task/.../node_modules
Error: 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.
Fix it fast
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.json2Match what you see, then jump to its fix
- 1
The 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
The package installs fine, but one file inside it is missing at runtimeThe file exists but was not included in the function bundleFix
- 1
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 backend2Fix
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/healthConfirmed by vercel.com
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/healthConfirmed by vercel.com
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 fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix