Logo

dev-resources.site

for different kinds of informations.

Deploying an Existing Express API + Prisma + Supabase Project to Vercel

Published at
1/1/2025
Categories
express
vercel
supabase
prisma
Author
7twilight
Categories
4 categories in total
express
open
vercel
open
supabase
open
prisma
open
Author
9 person written this
7twilight
open
Deploying an Existing Express API + Prisma + Supabase Project to Vercel

Deploying an existing Express API integrated with Prisma and Supabase to Vercel can be straightforward if done correctly. In this guide, we’ll cover the steps to deploy your project using the Vercel CLI (instead of GitHub Connect), the necessary configurations, and potential pitfalls to avoid.


Prerequisites

Before we start, ensure you have:

  • An existing project using Express, Prisma, and Supabase.
  • A Vercel account.
  • The Vercel CLI installed:
  npm install -g vercel
Enter fullscreen mode Exit fullscreen mode
  • Supabase database credentials.

Step 1: Prepare Your Project for Vercel

1.1 Ensure Your Entry Point is Serverless Compatible

Vercel’s serverless environment works differently from traditional Node.js servers. Modify your server.js or app.js to export the Express app as a module. For example:

const express = require('express');
const app = express();

// Your routes
app.get('/', (req, res) => {
  res.send('Hello, Vercel!');
});

module.exports = app;
Enter fullscreen mode Exit fullscreen mode

Then create a vercel.json file to define the serverless configuration:

{
  "version": 2,
  "builds": [
    {
      "src": "server.js",
      "use": "@vercel/node"
    }
  ],
  "routes": [
    {
      "src": "/(.*)",
      "dest": "/server.js"
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

1.2 Prisma Client Initialization

Ensure your Prisma Client is initialized correctly to avoid multiple instances in a serverless environment. Use a singleton pattern:

const { PrismaClient } = require('@prisma/client');

let prisma;

if (!global.prisma) {
  global.prisma = new PrismaClient();
}
prisma = global.prisma;

module.exports = prisma;
Enter fullscreen mode Exit fullscreen mode

1.3 Add prisma generate to postinstall

To avoid runtime errors due to missing Prisma Client files in the serverless environment, add the prisma generate command to the postinstall script in your package.json:

{
  "scripts": {
    "postinstall": "prisma generate"
  }
}
Enter fullscreen mode Exit fullscreen mode

This ensures Prisma Client is always generated after dependencies are installed.


Step 2: Configure Supabase Connection

2.1 How to Get Your Database URL from Supabase

To retrieve your DATABASE_URL and DIRECT_URL from Supabase:

  1. Log in to your Supabase Dashboard.
  2. Select your project.
  3. Navigate to Project Settings > API > Connection string for Prisma.
  4. Under this section, you will find:
    • DATABASE_URL: This is the connection string for using Supabase's connection pooling.
    • DIRECT_URL: This is the direct connection string to your database for administrative tasks like migrations.
  5. Copy these values and store them securely.

2.2 Add DATABASE_URL and DIRECT_URL

In Vercel, environment variables are stored securely. Navigate to Project Settings > Environment Variables in your Vercel dashboard and add:

  • DATABASE_URL: This connects Prisma to Supabase using connection pooling.
  postgresql://<USER>:<PASSWORD>@<PROJECT>.pooler.supabase.com:6543/<DB_NAME>?pgbouncer=true
Enter fullscreen mode Exit fullscreen mode
  • DIRECT_URL: This allows direct connections for migrations.
  postgresql://<USER>:<PASSWORD>@<PROJECT>.supabase.co:5432/<DB_NAME>
Enter fullscreen mode Exit fullscreen mode

Ensure these variables match the ones defined in your schema.prisma file:

datasource db {
  provider   = "postgresql"
  url        = env("DATABASE_URL")
  directUrl  = env("DIRECT_URL")
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Deploy Using Vercel CLI

3.1 Login to Vercel CLI

Run the following command to log in:

vercel login
Enter fullscreen mode Exit fullscreen mode

3.2 Deploy the Project

Run the deployment command from your project’s root directory:

vercel
Enter fullscreen mode Exit fullscreen mode

Follow the interactive prompts to configure your project. Select the root folder and default settings if unsure.

3.3 Set Environment Variables via CLI

You can also set environment variables using the CLI:

vercel env add DATABASE_URL
vercel env add DIRECT_URL
Enter fullscreen mode Exit fullscreen mode

Paste your Supabase credentials when prompted.


Step 4: Run Prisma Migrations

To apply your Prisma migrations, use the Vercel CLI to spawn a shell on your deployment and run migrations:

vercel --prod --exec "npx prisma migrate deploy"
Enter fullscreen mode Exit fullscreen mode

This ensures your database schema is up to date without manual intervention.


Conclusion

Deploying an Express API with Prisma and Supabase to Vercel requires careful configuration of serverless patterns, environment variables, and database connections. By following the steps outlined here, you can ensure a smooth deployment process and avoid common pitfalls.

If you encounter any issues or have questions, feel free to share them in the comments below! 🚀

supabase Article's
30 articles in total
Favicon
Building Production-Grade Web Applications with Supabase – Part 1
Favicon
Tracing and Metrics in supabase/storage
Favicon
Understanding Storage backends in supabase/storage
Favicon
Auth in Supabase storage
Favicon
Overview of supabase storage repository
Favicon
Harnessing the Power of Self-Hosted Supabase on Coolify: A Complete Guide to Server Setup and OAuth Providers Integration
Favicon
checa como se hace una conexión entre nextjs y la autenticación de supabase
Favicon
Deploying an Existing Express API + Prisma + Supabase Project to Vercel
Favicon
Custom schema specific Supabase Server Component clients in Grida Form workspace
Favicon
Custom schema specific Supabase Client Component clients in Grida Form workspace
Favicon
Introducing Supabase Client Playground: The Ultimate Tool for Streamlined Query Testing
Favicon
hCaptcha, a bot detection tool, usage in Supabase and Chatwoot
Favicon
Securing Client-Side Routes in Next.js with Supabase
Favicon
How to Transfer PostgreSQL Database from Local to Supabase on macOS
Favicon
Supabase | My Way of Designing & Managing DB
Favicon
What does Supabase use for its Authentication?
Favicon
É seguro guardar dados do usuário no localStorage?
Favicon
Supabase RLS Alternative
Favicon
Build Queue Worker using Supabase Cron, Queue and Edge Function
Favicon
Supabase Just Got More Powerful: Queue, Cron, and Background Tasks in Edge Functions
Favicon
Comparison of the middleware implementation between Supabase Auth documentation and the nextjs-stripe-supabase.
Favicon
How Supabase implemented micro-frontends using Multi Zones in Next.js
Favicon
Unlocking User Data: Building a Secure Supabase Edge Function
Favicon
Usecase: TumbleLog
Favicon
Supabase Edge Functions
Favicon
Setup Astro With Supabase and Prisma
Favicon
We launched SupaCharts! Visualize Beautiful Charts from your Supabase Data.
Favicon
A Free Minimalistic SaaS Starter Kit for Fast MVP Building
Favicon
supabase注册并创建项目和添加数据表
Favicon
Integrating GitHub Authentication with NextAuth.js: A Step-by-Step Guide

Featured ones: