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! 🚀

express Article's
30 articles in total
Favicon
Cookies auto clearing after browser refresh issue , CORS related express cookies issue
Favicon
how to setup express api from scratch
Favicon
I Really like Middleware in NodeJs/Express.
Favicon
From Legacy to Lightning: Modernizing an Astro App with Daytona
Favicon
Setting Up Dual Compilation (SSR + CSR) in ViteJS with vite-plugin-builder
Favicon
Starting A Series On TypeScript Join In As We Build Together
Favicon
How to Generate a Secure JWT Secret Using Node.js
Favicon
Mastering Express.js: A Deep Dive
Favicon
Comprendre le Design Pattern MVC avec Node.js, Express et MongoDB
Favicon
How to implement File uploads in Nodejs: A step by step guide
Favicon
Node backend port band bo'lib qolishi
Favicon
Deploying an Existing Express API + Prisma + Supabase Project to Vercel
Favicon
New React Library: API Integration Made Easy with Axiosflow's Automatic Client Generation
Favicon
Build and Deploy a Monorepo WebSocket web application with Turbo, Express, and Vite on Render Using Docker
Favicon
Express app with decorators based routing and dependency injection
Favicon
miniframe-router: Router for Express.JS Applications
Favicon
Create an API for AG-Grid with Express
Favicon
Blogsphere | A blogging website made with MERN stack. includes user management.
Favicon
วิธีทำ Auth API ด้วย Express, JWT, MySQL และ Prisma
Favicon
[Boost]
Favicon
Complete, full-stack setup for any node/express/psql app, equipped with basic ui, routes & more.
Favicon
Which One Should You Choose NEST JS or EXPRESS JS?
Favicon
Hackers Love These Common MERN Stack Mistakes: Are You Exposing Your App? 🔐
Favicon
Build a React login page template
Favicon
A New Way of Setting Up an Express Server for Lazy Developers npm i mbfi
Favicon
[Boost]
Favicon
Express request types
Favicon
Mastering Express.js: A Deep Dive
Favicon
Getting Started with Express.js for Web Development
Favicon
Introduction to TypeScript with Express: Building Your First REST API

Featured ones: