Prisma Client is an auto-generated database client that's tailored to your database schema. By default, Prisma Client is generated into the node_modules/@prisma/client folder, but you can specify a custom location.

To generate and instantiate Prisma Client:

  1. Ensure that you have Prisma CLI installed on your machine.

  2. Add the following generator definition to your Prisma schema:

    generator client {
    provider = "prisma-client-js"
    }
  3. Install the @prisma/client npm package:

    $npm install @prisma/client
  4. Generate Prisma Client with the following command:

    $prisma generate
  5. You can now instantiate the Prisma Client in your code:

    TypeScript
    JavaScript
    import { PrismaClient } from '@prisma/client'
    const prisma = new PrismaClient()
    // use `prisma` in your application to read and write data in your DB

Important: You need to re-run the prisma generate command after every change that's made to your Prisma schema to update the generated Prisma Client code.

Here is a graphical illustration of the typical workflow for the Prisma Client generation:

Graphical illustration of the typical workflow for the Prisma Client generation

Note also that prisma generate is automatically invoked when you're installing the @prisma/client npm package. So, when you're initially setting up Prisma Client, you can typically save the third step from the list above.

The @prisma/client npm package

The @prisma/client npm package consists of two key parts:

  • The @prisma/client module itself, which only changes when you re-install the package
  • The .prisma/client folder, which is the default location for the unique Prisma client generated from your schema

@prisma/client/index.d.ts exports .prisma/client:

export * from '.prisma/client'

This means that you still import @prisma/client in your own .ts files:

import { PrismaClient } from '@prisma/client'

The Prisma client is generated from your Prisma schema and is unique to your project. Each time you change the schema (for example, by performing a schema migration) and run prisma generate, the client code changes:

The .prisma and @prisma folders

The .prisma folder is unaffected by pruning in Node.js package managers.

The location of Prisma Client

If you do not specify a custom output in the generator block, Prisma Client is generated into the ./node_modules/.prisma/client folder by default. There are some advantages to maintaining the default location.

Using a custom output path

You can also specify a custom output path on the generator configuration, for example:

generator client {
provider = "prisma-client-js"
output = "./src/generated/client"
}

After running prisma generate for that schema file, the Prisma Client package will be located in:

./src/generated/client

To import the PrismaClient from a custom location:

import { PrismaClient } from '../prisma/src/generated/client'

Why is Prisma Client generated into node_modules/.prisma/client by default?

Importing Prisma Client

By generating Prisma Client into node_modules/.prisma/client and exporting it from node_modules/@prisma/client, you can import it and instantiate the client in your code as follows:

import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB

or

const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB

Keeping the query engine binary out of version control by default

Prisma Client is based on a query engine that's running as a sidecar process alongside your application. This query engine binary is downloaded when prisma generate is invoked and stored in the output path.

By generating Prisma Client into node_modules, the query engine is kept out of version control by default (since node_modules is typically ignored for version control). If it was not generated into node_modules, then you would need to explicitly ignore it, e.g. for Git you'd need to add the output path to your .gitignore file.

Generating Prisma Client in the postinstall hook of @prisma/client

The @prisma/client package defines its own postinstall hook that's being executed whenever the package is being installed. This hook invokes the prisma generate command which in turn generates the Prisma Client code into the default location node_modules/.prisma/client. Notice that this requires the prisma to be available, either as local dependency or as a global installation (it is recommended to always install the prisma package as a development dependency, using npm install prisma --save-dev, to avoid versioning conflicts though).

Edit this page on GitHub