How to use Prisma ORM with Turborepo _ Prisma Documentation
How to use Prisma ORM with Turborepo _ Prisma Documentation
Prisma is a powerful ORM for managing databases, and Turborepo simplifies monorepo
workflows. By combining these tools, you can create a scalable, modular architecture for
your projects.
This guide will show you how to set up Prisma as a standalone package in a Turborepo
monorepo, enabling efficient configuration, type sharing, and database management
across multiple apps.
NOTE
This guide was tested using Turborepo version 2.3.3 and Prisma ORM version
6.1.0 .
After the setup, choose a package manager for the project. Navigate to the project root
directory and install Turborepo as a development dependency:
$ cd ./hello-world
$ npm install turbo --save-dev Ask AI
https://www.prisma.io/docs/guides/turborepo#1-create-your-monorepo-using-turborepo 1/8
17/03/2025, 02:28 How to use Prisma ORM with Turborepo | Prisma Documentation
For more information about installing Turborepo, refer to the official Turborepo guide .
$ cd packages/
$ mkdir database
$ cd database
$ touch package.json
{
"name": "@repo/db",
"version": "0.0.0"
}
Next, install the required dependencies to use Prisma ORM. Use your preferred package
manager:
schema.prisma is where your Prisma schema lives. Here, you'll be able to modify the
shape of your database. The prisma init command by default will create a
https://www.prisma.io/docs/guides/turborepo#1-create-your-monorepo-using-turborepo 2/8
17/03/2025, 02:28 How to use Prisma ORM with Turborepo | Prisma Documentation
configuration for PostgreSQL to be used. You can modify the schema to use any other
supported database by Prisma ORM.
.gitignore adds some ignored files to git
.env lets you manually specify your DATABASE_URL for prisma.
WARNING
Make sure to replace the DATABASE_URL inside packages/database/.env with a
valid database url.
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
output = "../generated/client"
}
model User {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
email String @unique
name String?
}
In the schema.prisma file, we specify a custom output path where Prisma will generate
its types. This ensures Prisma's types are resolved correctly across different package
managers.
https://www.prisma.io/docs/guides/turborepo#1-create-your-monorepo-using-turborepo 3/8
17/03/2025, 02:28 How to use Prisma ORM with Turborepo | Prisma Documentation
{
"scripts": {
"db:generate": "prisma generate",
"db:migrate": "prisma migrate dev --skip-generate",
"db:deploy": "prisma migrate deploy"
}
}
{
"tasks": {
"db:generate": {
"cache": false
},
"db:migrate": {
"cache": false,
"persistent": true // This is necessary to interact with the CLI and assi
},
"db:deploy": {
"cache": false
}
}
}
Navigate to the project root and run the following command to automatically migrate our
database:
To generate the types from Prisma schema, from the project root run:
https://www.prisma.io/docs/guides/turborepo#1-create-your-monorepo-using-turborepo 4/8
17/03/2025, 02:28 How to use Prisma ORM with Turborepo | Prisma Documentation
In the packages/database directory, create a src folder and add a client.ts file. This
file will define an instance of PrismaClient :
packages/database/src/client.ts
Then create an index.ts file in the src folder to re-export the generated prisma types
and the PrismaClient instance:
packages/database/src/index.ts
Follow the Just-in-Time packaging pattern and create an entrypoint to the package
inside packages/database/package.json :
{
"exports": {
".": "./src/index.ts"
}
}
By completing these steps, you'll make the Prisma types and PrismaClient instance
accessible throughout the monorepo.
https://www.prisma.io/docs/guides/turborepo#1-create-your-monorepo-using-turborepo 5/8
17/03/2025, 02:28 How to use Prisma ORM with Turborepo | Prisma Documentation
The hello-world project should have an app called web at apps/web . Add the database
dependency to apps/web/package.json :
{
"dependencies": {
"@repo/db": "*"
}
}
Run your package manager's install command inside the apps/web directory:
$ cd apps/web
$ npm install
Let's import the intantiated prisma client from the database package in the web app in
the page.tsx file:
Then create a .env file in the web directory and copy of the contents of the .env file in
the database directory containing the DATABASE_URL .:
NOTE
If you want to use a single .env file in the root directory across your apps and
packages in a Turborepo setup, consider using a package like dotenvx .
https://www.prisma.io/docs/guides/turborepo#1-create-your-monorepo-using-turborepo 6/8
17/03/2025, 02:28 How to use Prisma ORM with Turborepo | Prisma Documentation
To implement this, update the package.json files for each package or app to
ensure they load the required environment variables from the shared .env file. For
detailed instructions, refer to the dotenvx guide for Turborepo .
If a new developer runs turbo dev on an application without first running db:generate ,
they will encounter errors.
To prevent this, ensure that db:generate is always executed before running dev or
build . Additionally, make sure both db:deploy and db:generate are executed before
db:build . Here's how to configure this in your turbo.json file:
{
"tasks": {
"dev": {
"dependsOn": ["^db:generate"],
"cache": false
// Additional configuration for dev tasks
},
"build": {
"dependsOn": ["^db:generate"],
// Additional configuration for build tasks
}
}
}
WARNING
Before starting the development server, note that if you are using Next.js v15.2.0,
do not use Turbopack as there is a known issue . Remove Turbopack from your
dev script by updating your apps/web/package.json
apps/web/package.json
https://www.prisma.io/docs/guides/turborepo#1-create-your-monorepo-using-turborepo 7/8
17/03/2025, 02:28 How to use Prisma ORM with Turborepo | Prisma Documentation
"script":{
- "dev": "next dev --turbopack --port 3000",
+ "dev": "next dev --port 3000",
}
NOTE
You can add users to your database by creating a seed script or manually by using
Prisma Studio.
To use Prisma Studio to add manually data via a GUI, navigate inside the
packages/database directory and run prisma studio using your package
manager:
https://www.prisma.io/docs/guides/turborepo#1-create-your-monorepo-using-turborepo 8/8