Deploying express app to Vercel

Hello everyone,

I’m trying to deploy an Express application to Vercel. My express project is a little different from the templates provided, it generates multiple files when I build the Typescript code.

Here’s my vercel.json file:

{
  "version": 2,
  "installCommand": "npm install",
  "builds": [
    {
      "src": "package.json",
      "use": "@vercel/node",
      "config": {
        "buildCommand": "npm run vercel-build"
      }
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "dist/server.js"
    }
  ]
}

When I run npm run vercel-build, one of the steps builds the typescript code into a dist directory.
But when I check the generated ‘Output’ on the deployment’s Source section. I can only see the package.json and middleware files.

I have tried to solve the issues using Vercel’s support chat bot, but after a while we started walking in circles with no success.

Below is the ‘dist’ folder from build output generated locally.

Anyone have a clue on how to solve the issue?
Thanks in advance.

UPDATE:
It seems like I don’t need to compile ts code, Vercel takes care of it.
So I removed the tsc build command from vercel-build and my vercel.json file now looks like this:

{
  "version": 2,
  "installCommand": "npm install",
  "buildCommand": "npm run vercel-build",
  "rewrites": [{ "source": "/(.*)", "destination": "/src" }]
}

PS: The typescript code is in /src directory.
Results: Still getting 404: NOT_FOUND error.

If you’re having trouble deploying an Express app, this guide can help.

You can also ask v0 for suggestions tailored to your own project setup.

1 Like

Hi @yuriarthf, welcome to the Vercel Community!

For your application to be considered a serverless API, you must have the files under the /api folder.

I’d recommend reading through the Express 101: Everything about deploying your Express app on Vercel post.

Let me know if you need more help.

1 Like

Hello @anshumanb,

Thanks for the help. I’ve changed the typescript src/ directory to api/.
Now I’m getting another problem. vercel-build command is being ran multiple times.
Any clue how I could solve it?

Hi @yuriarthf, can you share the logs? Basically all files in the api/ are handled to be a serverless function. So, maybe you can just keep the main server file inside api/ and import the other files from the root directory structure.

Hey @anshumanb,

Would be better if I could leave the structure as it’s, since it’s ready to be deployed into other platforms. I pointed out the rewrites to dest: "/api/server.ts", the one file that exports the express server. index.ts` runs the server.

The build logs are quite big, since it ran the same vercel-build command over again, in the end it says it has more than 12 functions, which is not true, I suspect it’s because it compiled the same function many times.

I’ll post the two last times it ran the vercel-build command, but again, there’re many more times, about 1240 lines.

[17:14:00.120] 2 migrations found in prisma/migrations
[17:14:00.120] 
[17:14:01.739] 
[17:14:01.739] No pending migrations to apply.
[17:14:01.794] Done in 4.17s.
[17:14:01.835] Using TypeScript 5.1.6 (local user-provided)
[17:14:02.260] Warning: Detected "engines": { "node": ">=16.0.0" } in your `package.json` that will automatically upgrade when a new major Node.js Version is released. Learn More: http://vercel.link/node-version
[17:14:02.273] Running "yarn run vercel-build"
[17:14:02.445] yarn run v1.22.19
[17:14:02.484] $ npm run prisma:generate && npm run prisma:migrate
[17:14:02.605] 
[17:14:02.605] > seal-api@2.0.4 prisma:generate
[17:14:02.606] > npx prisma generate
[17:14:02.606] 
[17:14:03.173] Prisma schema loaded from prisma/schema.prisma
[17:14:03.399] 
[17:14:03.399] ✔ Generated Prisma Client (5.1.1 | library) to ./node_modules/@prisma/client in 71ms
[17:14:03.400] You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client
[17:14:03.400] ```
[17:14:03.400] import { PrismaClient } from '@prisma/client'
[17:14:03.400] const prisma = new PrismaClient()
[17:14:03.400] ```
[17:14:03.605] 
[17:14:03.607] > seal-api@2.0.4 prisma:migrate
[17:14:03.607] > npx prisma migrate deploy
[17:14:03.607] 
[17:14:04.214] Prisma schema loaded from prisma/schema.prisma
[17:14:04.219] Datasource "db": PostgreSQL database "postgres", schema "public" at "aws-0-us-west-1.pooler.supabase.com:5432"
[17:14:04.985] 
[17:14:04.985] 2 migrations found in prisma/migrations
[17:14:04.985] 
[17:14:05.933] 
[17:14:05.933] No pending migrations to apply.
[17:14:05.987] Done in 3.54s.
[17:14:06.023] Using TypeScript 5.1.6 (local user-provided)
[17:14:07.752] Warning: Detected "engines": { "node": ">=16.0.0" } in your `package.json` that will automatically upgrade when a new major Node.js Version is released. Learn More: http://vercel.link/node-version
[17:14:07.763] Running "yarn run vercel-build"
[17:14:07.953] yarn run v1.22.19
[17:14:07.995] $ npm run prisma:generate && npm run prisma:migrate
[17:14:08.131] 
[17:14:08.132] > seal-api@2.0.4 prisma:generate
[17:14:08.132] > npx prisma generate
[17:14:08.132] 
[17:14:08.734] Prisma schema loaded from prisma/schema.prisma
[17:14:08.990] 
[17:14:08.991] ✔ Generated Prisma Client (5.1.1 | library) to ./node_modules/@prisma/client in 81ms
[17:14:08.991] You can now start using Prisma Client in your code. Reference: https://pris.ly/d/client
[17:14:08.991] ```
[17:14:08.992] import { PrismaClient } from '@prisma/client'
[17:14:08.992] const prisma = new PrismaClient()
[17:14:08.992] ```
[17:14:09.206] 
[17:14:09.206] > seal-api@2.0.4 prisma:migrate
[17:14:09.207] > npx prisma migrate deploy
[17:14:09.207] 
[17:14:09.790] Prisma schema loaded from prisma/schema.prisma
[17:14:09.794] Datasource "db": PostgreSQL database "postgres", schema "public" at "aws-0-us-west-1.pooler.supabase.com:5432"
[17:14:10.480] 
[17:14:10.481] 2 migrations found in prisma/migrations
[17:14:10.481] 
[17:14:11.459] 
[17:14:11.459] No pending migrations to apply.
[17:14:11.510] Done in 3.56s.
[17:14:11.550] Using TypeScript 5.1.6 (local user-provided)
[17:14:13.345] Build Completed in /vercel/output [3m]
[17:14:14.017] Deploying outputs...
[17:14:20.190] Error: No more than 12 Serverless Functions can be added to a Deployment on the Hobby plan. Create a team (Pro plan) to deploy more. Learn More: https://vercel.link/function-count-limit
[17:14:20.670] 

Hi @yuriarthf, I understand the reservation to not change the directories but that’s how Vercel handles API routes.

If you need more help, please share your public repo or a minimal reproducible example. That will let us all work together from the same code to figure out what’s going wrong.

Hey @anshumanb,

Thanks for your help!
I was able to successfully deploy the app by placing the /api folder in the root directory and also by compiling typescript. It didn’t work well when I pointed directly to a typescript file.

3 Likes

Hi @yuriarthf, glad to see it is working now. Also, thanks for sharing the solution here.

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