DEV Community

Cover image for Fixing Vercel 404 Errors: A Simple Solution for Node.js Projects
Elliot Brenya sarfo
Elliot Brenya sarfo

Posted on

3 1 1 1 1

Fixing Vercel 404 Errors: A Simple Solution for Node.js Projects

I recently ran into a frustrating issue with my Node.js project deployed on Vercel where all routes except the home page (/) were returning 404 errors. After hours of debugging and searching through StackOverflow, I finally found a simple solution that I wanted to share with the community.

The Problem

My GitHub Activity Generator project was working perfectly fine locally, but after deploying to Vercel, any route refresh would result in a 404 error. The home page would load initially, but refreshing any other route would break the application. This is a common issue that occurs when the routing configuration isn't properly set up for single-page applications on Vercel.

The Solution

The fix turned out to be surprisingly simple. It involved two key steps:

  1. Creating a minimal vercel.json configuration file
{
  "rewrites": [
    {
      "source": "/(.*)",
      "destination": "/"
    }
  ],
  "buildCommand": "npm run build",
  "outputDirectory": "dist"
}
Enter fullscreen mode Exit fullscreen mode

Setting the Framework Preset to "Node.js" in the Vercel project settings.

Why This Works

The magic happens in the rewrites configuration. The catch-all route "source": "/(.*)" captures all incoming requests and redirects them to the root ("destination": "/"). This ensures that our Node.js application handles the routing instead of letting Vercel try to find static files for each route.

Implementation Steps

Here's how I implemented the fix:

  1. First, I created a vercel.json file in my project root
  2. Added the minimal configuration shown above
  3. Pushed the changes to my GitHub repository
  4. In my Vercel dashboard, I went to Project Settings
  5. Under "Framework Preset", selected "Node.js"
  6. Redeployed the application

And just like that, all my routes started working perfectly! No more 404 errors on refresh.

What I Learn

  1. Less is more when it comes to Vercel configuration
  2. The framework preset selection is crucial for proper routing
  3. A simple rewrite rule can solve complex routing issues
  4. If you're facing similar 404 issues with your Vercel deployment, try this solution.
  5. It's worked wonders for my project, and I hope it helps you too.

Neon image

Serverless Postgres in 300ms (!)

10 free databases with autoscaling, scale-to-zero, and read replicas. Start building without infrastructure headaches. No credit card needed.

Try for Free →

Top comments (1)

Collapse
 
keshavsingh123 profile image
Keshav singh

framework preset does't have nodejs

Jetbrains image

Is Your CI/CD Server a Prime Target for Attack?

57% of organizations have suffered from a security incident related to DevOps toolchain exposures. It makes sense—CI/CD servers have access to source code, a highly valuable asset. Is yours secure? Check out nine practical tips to protect your CI/CD.

Learn more

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay