Introduction
Working with serverless functions on platforms like Vercel can be a seamless experience, but occasionally, challenges may arise. Recently, I encountered a serverless function crash on Vercel, and I'm here to share how I resolved it.
The Problem
The issue manifested as a 500 Internal Server Error, and the logs pointed towards a missing module: Cannot find module 'express'. This error message was puzzling as express was part of the project dependencies. The Error code looks like this,
`Cannot find module 'express'
Require stack:
- /var/task/index.js
- /var/task/___now_launcher.js
- /var/runtime/index.mjs
Did you forget to add it to "dependencies" in
package.json
? INIT_REPORT Init Duration: 202.73 ms Phase: invoke Status: error Error Type: Runtime.ExitError Error: Runtime exited with error: exit status 1 Runtime.ExitError`
Troubleshooting Steps
- Dependency Check
First, I double-checked my
package.json
file to ensure that express was listed as a dependency. Surprisingly, everything seemed fine.
"dependencies": {
"express": "^4.18.2"
},
- Vercel Configuration
Next, I revisited the
vercel.json
file. The configuration for serverless functions is crucial here. Previousconfig
was
{
"version": 2,
"builds": [
{
"src": "index.js",
"use": "@now/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "index.js"
}
]
}
The Solution
{
"version": 2,
"builds": [
{
"src": "index.js",
"use": "@vercel/node"
}
],
"routes": [
{
"src": "/(.*)",
"dest": "/",
"methods": [
"GET",
"POST",
"PUT",
"PATCH",
"DELETE",
"OPTIONS"
]
}
]
}
Top comments (1)
πππ