Skip to main content
Prisma Documentation Docs

Search documentation

Type to search this documentation.

On this pageOverview

orm init

orm init scaffolds the Prisma ORM config, contract source, and runtime files inside an existing project, installs dependencies, and emits the contract. It gets you from zero to typed queries in one step.

Use a Prisma ORM quickstart when you want a complete new application template. Use orm init when you already have a project and want to add the lower-level Prisma ORM files.

Run it interactively for a guided setup:

title="bun"
bunx prisma@latest orm init
pnpm
pnpm dlx prisma@latest orm init
yarn
yarn dlx prisma@latest orm init
npm
npx prisma@latest orm init

Or supply --target and --authoring for a fully scriptable run (CI, AI coding agents, automation):

title="bun"
bunx prisma@latest orm init --yes --target postgres --authoring psl
pnpm
pnpm dlx prisma@latest orm init --yes --target postgres --authoring psl
yarn
yarn dlx prisma@latest orm init --yes --target postgres --authoring psl
npm
npx prisma@latest orm init --yes --target postgres --authoring psl
Option What it does
--target <db> Sets the database target. Use postgres or mongodb.
--authoring <style> Sets the contract authoring style. Use psl or typescript.
--schema-path <path> Sets where the starter contract is written.
--from-prisma7-schema <path> Uses the Prisma ORM 7 schema at <path> as the contract source instead of writing a starter contract. PostgreSQL only. See On a Prisma ORM 7 project.
--write-env Writes .env from .env.example (gitignored).
--probe-db Connects to DATABASE_URL once and checks the server version.
--strict-probe Treats a failed database probe as fatal.
--skip-install Skips dependency installation and contract emission.
--keep-previous-facade Keeps the previous target package in package.json when switching targets.

The exact files depend on the target and authoring style, but a Postgres PSL setup normally includes:

  • prisma.config.ts
  • a starter contract file such as src/prisma/contract.prisma, whose first line is // use prisma-8. Keep that line, because contract emit reads only .prisma files that start with it.
  • emitted contract artifacts after installation runs
  • a runtime client file (src/prisma/db.ts) that imports contract.json
  • .env.example, tsconfig.json, and updated package.json
  • prisma-8.md, a short quick reference for writing your first typed query

The install step adds the target package and dotenv to dependencies. It adds prisma@latest, @types/node, and the matching @prisma/cli-engine to devDependencies.

orm init also changes the module settings in tsconfig.json and can add "type": "module" to package.json. If your app is CommonJS, read In a CommonJS project before you run the app again.

The generated prisma.config.ts imports definePrismaConfig from @prisma/cli-engine, while projects created by create-prisma (and the examples in Configuration) import it from prisma/config. Both work; keep the import your project already has when you copy a config example.

A CommonJS project is one where your code loads other files with require, or where tsc compiles your import lines to require calls. orm init changes package.json and tsconfig.json in ways that stop a CommonJS app from starting. This list says what it changes, and the sections after it say what to change back before you run the app again:

  • In package.json, orm init adds "type": "module" when the file has no "type" field, and Node.js then loads every .js file in the project as an ES module. When the file already declares "type": "commonjs", orm init keeps it and prints a warning.
  • In tsconfig.json, orm init sets "module": "preserve" and "moduleResolution": "bundler" in place of the values you had, and adds "resolveJsonModule": true.

What you do next depends on how you run the app.

If orm init added "type": "module" to package.json, remove that line. Change nothing else. If you also compile with tsc for production, follow the next section instead.

With the files as orm init leaves them, the compiled app does not start: node stops with ERR_MODULE_NOT_FOUND, or with SyntaxError: Cannot use import statement outside a module if your package.json declares "type": "commonjs". To keep the app CommonJS, make the following changes. They also work if you run the app through tsx while you develop.

  1. If orm init added "type": "module" to package.json, remove that line.

  2. In tsconfig.json, set module and moduleResolution to nodenext, and set skipLibCheck to true:

    (excerpt)"
    {
    
      "compilerOptions": {
    
        "module": "nodenext",
    
        "moduleResolution": "nodenext",
    
        "resolveJsonModule": true,
    
        "skipLibCheck": true
    
      }
    
    }
  3. In src/prisma/db.ts, remove with { type: 'json' } from the line that imports contract.json:

    (excerpt)"
    import contractJson from './contract.json';

With nodenext, tsc still writes CommonJS output, because your package.json does not say "type": "module". Your relative imports stay as they are, without file extensions, and tsc copies contract.json into the output directory beside db.js. prisma contract emit does not rewrite db.ts, so you make that edit once.

Do not restore "module": "commonjs" with "moduleResolution": "node", because tsc cannot find the Prisma ORM packages with those values.

Keep "type": "module" in package.json, or set it if orm init kept "type": "commonjs", and leave the scaffolded files as they are. Every file in the project then runs as an ES module, so each file that uses require, module.exports, or __dirname needs rewriting.

orm init can set up Prisma ORM 8 beside Prisma ORM 7 on PostgreSQL, with Prisma ORM 8 reading your existing schema.prisma as its contract source. Pass the schema path:

Bash
bunx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma
Terminal
pnpm dlx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma
Terminal
yarn dlx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma
Terminal
npx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma

A plain orm init offers the same setup when it finds a Prisma ORM 7 config, or a prisma/schema.prisma with a datasource block. The database target comes from the schema's datasource provider, and a --target that disagrees with it fails with CLI.INIT_PRISMA7_TARGET_MISMATCH.

First, orm init installs @prisma/orm-postgres and dotenv, adding them to package.json if they are not there yet, and uses the installed package to check that Prisma ORM 8 can read the schema. Apart from that install, it changes nothing until the check passes. A schema it cannot read, for example one with a view block, stops the command with CLI.INIT_PRISMA7_SCHEMA_REFUSED, which lists each problem with its line and the Prisma ORM 7 edit that removes it, and gives the command that removes the packages it just added.

When the check passes, orm init asks you to confirm by typing the name of the directory you run it in. In a script, where nobody can type, pass that name with --confirm, and add --no-interactive so that any other question takes its default answer. For a project in a directory named my-app:

A plain orm init offers the same setup when it finds a Prisma ORM 7 config, or a prisma/schema.prisma with a datasource block. The database target comes from the schema's datasource provider, and a --target that disagrees with it fails with CLI.INIT_PRISMA7_TARGET_MISMATCH.

First, orm init installs @prisma/orm-postgres and dotenv, adding them to package.json if they are not there yet, and uses the installed package to check that Prisma ORM 8 can read the schema. Apart from that install, it changes nothing until the check passes. A schema it cannot read, for example one with a view block, stops the command with CLI.INIT_PRISMA7_SCHEMA_REFUSED, which lists each problem with its line and the Prisma ORM 7 edit that removes it, and gives the command that removes the packages it just added.

When the check passes, orm init asks you to confirm by typing the name of the directory you run it in. In a script, where nobody can type, pass that name with --confirm, and add --no-interactive so that any other question takes its default answer. For a project in a directory named my-app:

bun
bunx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma --no-interactive --confirm my-app
pnpm
pnpm dlx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma --no-interactive --confirm my-app
yarn
yarn dlx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma --no-interactive --confirm my-app
npm
npx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma --no-interactive --confirm my-app

After you confirm, orm init:

  • renames the Prisma ORM 7 config to prisma7.config.ts and points its import at @prisma/prisma7/config
  • changes every package.json script that runs prisma to run prisma7
  • installs the latest Prisma ORM 7 release as @prisma/prisma7, next to prisma@latest
  • writes prisma.config.ts with contract: prisma7Schema("<schema path>"), plus src/prisma/db.ts and prisma-8.md

It does not write a starter contract, and it changes nothing in prisma/ or in the database. Prisma ORM 7 keeps owning the schema and its migrations. From now on, prisma runs Prisma ORM 8 and prisma7 runs Prisma ORM 7, so a Prisma ORM 7 command such as migrate dev becomes npx prisma7 migrate dev. Your application keeps using the Prisma ORM 7 client until you move its routes, one at a time, to the Prisma ORM 8 client in src/prisma/db.ts. If the project still uses a Prisma ORM version before 7, so that your package.json lists @prisma/client below version 7, orm init upgrades @prisma/client to version 7, because the Prisma ORM 7 CLI it installs needs a client of the same major version. Run npx prisma7 generate afterwards to generate the upgraded client.

To finish, set DATABASE_URL and run npx prisma db sign once. db sign checks that the database matches the contract and then writes the marker, a row that records which contract the database matches. The row goes in the table prisma_contract.marker, which the first db sign creates in a PostgreSQL schema of its own named prisma_contract, so your own tables do not change. After each Prisma ORM 7 migration, whether you ran npx prisma7 migrate dev or npx prisma7 migrate deploy, run npx prisma contract emit and then npx prisma db sign again. Configuration explains prisma7Schema, and the upgrade guide covers the rest of the move.

title="bun"
bunx prisma@latest orm init --yes --target postgres --authoring psl

bunx prisma@latest orm init --yes --target mongodb --authoring typescript --json

bunx prisma@latest orm init --skip-install
pnpm
pnpm dlx prisma@latest orm init --yes --target postgres --authoring psl
pnpm dlx prisma@latest orm init --yes --target mongodb --authoring typescript --json
pnpm dlx prisma@latest orm init --skip-install
yarn
yarn dlx prisma@latest orm init --yes --target postgres --authoring psl
yarn dlx prisma@latest orm init --yes --target mongodb --authoring typescript --json
yarn dlx prisma@latest orm init --skip-install
npm
npx prisma@latest orm init --yes --target postgres --authoring psl
npx prisma@latest orm init --yes --target mongodb --authoring typescript --json
npx prisma@latest orm init --skip-install

Review the generated files, set DATABASE_URL, and emit the contract when you change the schema:

bunx prisma contract emit
Bash
pnpm prisma contract emit
Bash
yarn prisma contract emit
Bash
npx prisma contract emit

Then initialize a new database, or verify one that Prisma ORM already set up. On a database that already has your tables, run db sign instead of db init:

Then initialize a new database, or verify one that Prisma ORM already set up. On a database that already has your tables, run db sign instead of db init:

bunx prisma db init --db "$DATABASE_URL"

bunx prisma db verify --db "$DATABASE_URL"
Bash
pnpm prisma db init --db "$DATABASE_URL"
pnpm prisma db verify --db "$DATABASE_URL"
Bash
yarn prisma db init --db "$DATABASE_URL"
yarn prisma db verify --db "$DATABASE_URL"
Bash
npx prisma db init --db "$DATABASE_URL"
npx prisma db verify --db "$DATABASE_URL"

The scaffolded db.ts starts with import 'dotenv/config', so any script that imports db reads DATABASE_URL from the .env file in the directory you run it from.

The scaffolded db.ts starts with import 'dotenv/config', so any script that imports db reads DATABASE_URL from the .env file in the directory you run it from.

Suggest an edit

Propose a replacement for this page. The site team reviews it before applying any changes.

Export
Documentation menu