Vercel dev locally not serving Functions

Hello! I have created a react native + expo project. I have an API working in production/preview/dev (when vercel hosts the code, it runs perfectly)

However, when I run vercel dev locally, it serves the website, but doesn’t serve any of the functions. I’ve been working on this problem for a couple hours and have tried all kinds of routing combinations and such, but i think the serverless functions simply aren’t being launched.

The same goes for running with Expo locally, it just doesn’t work. I’m not sure what’s different, maybe vercel is doing something more clever with the API hosting that isn’t being replicated locally.

Please help!

Locally, my website is rendering just fine. But any API calls receive a 404 response (unlike in production where the API goes through perfectly).

Hey @nollied. What are the Build and Development Settings for the project? The deployed version of the site would use the Build Commend, so the local issue might be caused by the Development Command.

If you have a minimal reproducible example or public repo, that would let get a better look at the issue. Short of that, seeing the vercel.json config for the project can help me narrow down the possible causes.

Hi @amyegan, thank you for the quick reply!

I don’t have a full minimal code example, but I can provide steps to reproduce:

  1. Create a new react-native project (typescript). No need to write any front-end code.
    • React Native should come with expo as the build system, and with the configurations for vercel.json set up + package.json, you should be able to run vercel dev to see the local server running.
  2. Create a vercel project and link with the react native project, entering in the custom build and development settings (in the screen shot)
    • Running vercel will deploy to the vercel project.

After deploying the vercel project, visiting the url https://{project_url}/api/health should result in a 200 json response.

However… if you were to run this project locally with vercel dev and then visit the link with http://localhost:8081/api/health, it will result in a Unmatched Route error.

Here’s the code for health.ts, it’s just a simple API that should return a JSON 200 response always. Make sure to create this file with the path my-project/api/health.ts

export async function GET(request: Request) {
    return new Response(JSON.stringify({ status: 'ok' }), {
        status: 200,
        headers: {
            'Content-Type': 'application/json',
        },
    });
}

The build and development settings are custom, here’s a screen shot:

Here’s my vercel.json:

{
  "buildCommand": "expo export -p web",
  "outputDirectory": "dist",
  "cleanUrls": true,
  "framework": null,
  "rewrites": [
    {
      "source": "/api/:path*",
      "destination": "/api/:path*"
    },
    {
      "source": "/:path*",
      "destination": "/"
    }
  ]
}

and my part of my package.json without the dependencies:

{
  "name": "xlate",
  "main": "expo-router/entry",
  "version": "1.0.0",
  "scripts": {
    "start": "expo start",
    "reset-project": "node ./scripts/reset-project.js",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "test": "jest --watchAll",
    "lint": "expo lint",
    "build": "expo export:web"
  },

... deps ...

i tried tuning the routes and moving the APIs around, but nothing seemed to work. There also seems to be a random timeout crashing the whole site when running locally, i wonder if it has to do with the functions not being launched?

@verbose-void ➜ /workspaces/xlate (improve-chat) $ vercel dev
Vercel CLI 39.1.0
> Running Dev Command “expo start --web”
Starting project at /workspaces/xlate/website
Starting Metro Bundler
Waiting on http://localhost:8081
Logs for your project will appear below.
λ Bundled 1803ms node_modules/expo-router/node/render.js (900 modules)
Web Bundled 2398ms node_modules/expo-router/entry.js (982 modules)
Web Bundled 508ms node_modules/expo-router/entry.js (909 modules)
 LOG  [web] Logs will appear in the browser console
λ Bundled 48ms node_modules/expo-router/node/render.js (1 module)
Web Bundled 485ms node_modules/expo-router/entry.js (1 module)
Web Bundled 38ms node_modules/expo-router/entry.js (1 module)
 LOG  [web] Logs will appear in the browser console
Error: Detecting port 42021 timed out after 300000ms
Error: An unexpected error occurred!
Error: Detecting port 42021 timed out after 300000ms
    at checkForPort (/usr/local/share/nvm/versions/node/v20.17.0/lib/node_modules/vercel/dist/index.js:143048:11)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async DevServer.runDevCommand (/usr/local/share/nvm/versions/node/v20.17.0/lib/node_modules/vercel/dist/index.js:144786:32)
    at async DevServer._getVercelConfig (/usr/local/share/nvm/versions/node/v20.17.0/lib/node_modules/vercel/dist/index.js:144211:9)

Thanks for sharing the repro steps. I was able to get an example working, but it needed some notable modification.

Whenever a framework has its own routing system, it’s best to take advantage of that. I suspect there was some disagreement between the Expo dev routing and Vercel dev routing. So I used the API route recommendations from the Expo docs.

I pushed my example to a public repo so you can see what I used. This setup works for local development with vercel dev and when deployed.

Repo: github.com/amyegan/expo-2183
Deployed example: expo-2183.vercel.app/api/health

I hope that helps!

@amyegan thank you again for your prompt responses!

however , I tried using the “server” switch as you did also- when I run locally, i’m still seeing the unmatched route.

moreover, when I deploy this site with the “server” option instead of “static”, vercel no longer even hosts the front-end.

I’ve been wrestling with this for a while now, i’ve pretty much tried every workaround except changing my repo to a root-based repo. i believe this to be the most likely culprit to the issue.

just to re-cap:

  1. when adding a custom root directory (assumed reason for bugs), using “server” output instead of “static” results in:
    • API still broken locally (but i’ve double checked that the API does now work when vercel deploys, but the API still doesn’t work locally with vercel dev)
    • web serving the front-end on vercel deploy results in 404, however locally web serving is working fine (but again, local APIs still don’t work).

this is a complicated bug so i’m sorry for the spaghetti logic with my explanations with the combinatorical edge cases.

thanks again.
-dyllan

I followed the Expo docs pretty closely to get it working in my example. Did you move the /api/health.ts file to /app/api/health+api.ts? That’ll let the framework’s routing system handle the path for you. A new /api/index.ts file was also needed to handle the server switch.

The Vercel documentation also includes some info about using monorepos. That’s worth reading through if you think a monorepo structure is part of the issue for your project.

It now works locally for both the front-end and backend, however when deploying to vercel cloud it’s giving a 404 for both the front-end and our health API :frowning:

I thought it was working but only locally now, i will take a closer look and hopefully try to fix deployments.

Also I tried pasting in your vercel.json which results in a 500 error on the front-end, and still a 404 on the health API.


now it’s kind of one or the other, i can get the functions/front end to run locally OR if i make some code changes i can get them to work in the vercel deploy, but not at the same time lol.

it’s just still not detecting those functions when it has the +api extension in vercel. only when i have local running does +api get detected.

but they are in the output files…

omg, I got it to finally work. the primary issue was actually the versions in my package.json for expo and react-native were pretty out of date. so i just upgraded everything to the latest and ported my project entirely to fit your repository’s layout.

now everything works locally AND in the cloud. thanks so much for your help!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.