Monorepo express setup

I’m fairly new to Vercel and am really struggling getting an Express server using Turborepo and internal packages to work.

In order for Vercel to create the server less function I’m re-exporting my express app from /apps/api/src/index.ts to /apps/api/api/index.ts and then rewrite requests using { "source": "/(.*)", "destination": "/api/index.ts" }. That caused issues on build however because for some reason Vercel couldn’t resolve the internal packages correctly. For that reason I’m now bundling everything instead and re-export /apps/api/dist/index.js.

When manually deploying the project using vercel --prod everything is building and working correctly, but deployments triggered by git pushes throw the following runtime error: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/var/task/apps/api/dist/index.js' imported from /var/task/apps/api/api/index.js. Clearly there seem to be differences between manual deployments and automatically triggered ones, but I have no idea what they are and how to resolve them.

PS: manually deploying also worked before I bundled the code DESPITE throwing errors in the build log. After the change the build errors disappeared, but as said, automatically triggered deployments are throwing the runtime error.

simplified directory structure

/apps
  /web
  /api
    /src
      index.ts (importing /packages/db)
    /dist
      index.js
    /api
      index.js (re-rexporting /apps/api/dist/index.js) 
/packages
  /db

vercel.json

{
  "$schema": "https://openapi.vercel.sh/vercel.json",
  "outputDirectory": "",
  "rewrites": [
    {
      "source": "/(.*)",
      "destination": "/api/index.js"
    }
  ]
}

Anyone an idea what I’m doing wrong or how this is supposed to work?

Hi, @hyperborea!

Have you taken a look at our Express guide?

Hi @pawlean! I have, that’s what I modelled my setup on. The big difference is that a) I’m using internal packages in Turborepo and b) I want to keep my code in src/ and only link to it in api/, which works when manually deploying using vercel --prod, but not when it automatically deploys when pushing to main.

Actually I just noticed that manually deploying using vercel --prod appears to NOT build the app, but uploads the local directory instead. That’s why it works, local builds are correct.

@pawlean can I somehow have it go through the same process as git-push deployments would and how can I debug / configure what’s exactly running?

Anyone can help me understand how the build process works on git-push deployments? Neither the /dist folder nor dependent turbo packages show up for me and I don’t know how to debug this further.

I suspect Vercel might simply not be for me or my use case. Too much magic under the hood with the /api folder convention.

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