DEV Community

Cover image for How to Solve Vercel Server Less function Crushed 500 Error for Express Server.
Farhan Hasan Nilok
Farhan Hasan Nilok

Posted on

How to Solve Vercel Server Less function Crushed 500 Error for Express Server.

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.

Vercel Error Image

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

  1. 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"
},

  1. Vercel Configuration Next, I revisited the vercel.json file. The configuration for serverless functions is crucial here. Previous config was
{
  "version": 2,
  "builds": [
    {
      "src": "index.js",
      "use": "@now/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "index.js"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

The Solution

{
    "version": 2,
    "builds": [
        {
            "src": "index.js",
            "use": "@vercel/node"
        }
    ],
    "routes": [
        {
            "src": "/(.*)",
            "dest": "/",
            "methods": [
                "GET",
                "POST",
                "PUT",
                "PATCH",
                "DELETE",
                "OPTIONS"
            ]
        }
    ]
}

Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
nehaltrio profile image
Ahmed Ajmine Nehal

👏👏👏