# 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](/guides/prisma-orm-2-prisma-orm-quickstart-postgresql) 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.

## [Usage](#usage)

Run it interactively for a guided setup:

:::code-group
```title="bun"
bunx prisma@latest orm init
```

```bash title="pnpm"
pnpm dlx prisma@latest orm init
```

```bash title="yarn"
yarn dlx prisma@latest orm init
```

```bash title="npm"
npx prisma@latest orm init
```
:::

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

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

```bash title="pnpm"
pnpm dlx prisma@latest orm init --yes --target postgres --authoring psl
```

```bash title="yarn"
yarn dlx prisma@latest orm init --yes --target postgres --authoring psl
```

```bash title="npm"
npx prisma@latest orm init --yes --target postgres --authoring psl
```
:::

## [Options](#options)

| 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](#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.                                                                                                          |

## [What it creates](#what-it-creates)

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](#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](/guides/introduction-6-configuration)) import it from `prisma/config`. Both work; keep the import your project already has when you copy a config example.

## [In a CommonJS project](#in-a-commonjs-project)

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.

### [You run the app through `tsx`](#you-run-the-app-through-tsx)

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.

### [You compile with `tsc` and run the output with `node`](#you-compile-with-tsc-and-run-the-output-with-node)

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`:

   ```title="tsconfig.json (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`:

   ```title="src/prisma/db.ts (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.

### [You want the app to run as ES modules](#you-want-the-app-to-run-as-es-modules)

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.

## [On a Prisma ORM 7 project](#on-a-prisma-orm-7-project)

`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:

::::tabs
:::tab{title="bun"}
```bash
bunx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma
```
:::

:::tab{title="pnpm"}
```bash title="Terminal"
pnpm dlx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma
```
:::

:::tab{title="yarn"}
```bash title="Terminal"
yarn dlx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma
```
:::

:::tab{title="npm"}
```bash title="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`:

:::code-group
```bash title="bun"
bunx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma --no-interactive --confirm my-app
```

```bash title="pnpm"
pnpm dlx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma --no-interactive --confirm my-app
```

```bash title="yarn"
yarn dlx prisma@latest orm init --from-prisma7-schema prisma/schema.prisma --no-interactive --confirm my-app
```

```bash title="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`](/guides/orm-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](/guides/introduction-6-configuration#use-a-prisma-orm-7-schema) explains `prisma7Schema`, and [the upgrade guide](/guides/upgrade-prisma-orm-postgresql) covers the rest of the move.

## [Examples](#examples)

:::code-group
```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
```

```bash title="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
```

```bash title="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
```

```bash title="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
```
:::

## [After initialization](#after-initialization)

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

::::tabs
:::tab{title="bun"}
```
bunx prisma contract emit
```
:::

:::tab{title="pnpm"}
```bash
pnpm prisma contract emit
```
:::

:::tab{title="yarn"}
```bash
yarn prisma contract emit
```
:::

:::tab{title="npm"}
```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`](/guides/orm-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`](/guides/orm-db-sign) instead of `db init`:

::::tabs
:::tab{title="bun"}
```
bunx prisma db init --db "$DATABASE_URL"

bunx prisma db verify --db "$DATABASE_URL"
```
:::

:::tab{title="pnpm"}
```bash
pnpm prisma db init --db "$DATABASE_URL"
pnpm prisma db verify --db "$DATABASE_URL"
```
:::

:::tab{title="yarn"}
```bash
yarn prisma db init --db "$DATABASE_URL"
yarn prisma db verify --db "$DATABASE_URL"
```
:::

:::tab{title="npm"}
```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.

## Related pages

- [Authentication & Tools](./authentication-tools-index.md)
- [Build](./build-index.md)
- [Changelog](../changelog.md)
- [Concepts](./concepts-index.md)
- [Console commands](./console-commands-index.md)
- [Contract Authoring](./contract-authoring-index.md)
- [Core Concepts](./core-concepts-index.md)
- [Data Modeling](./data-modeling-index.md)
- [Database](./database-index.md)
- [DB commands](./db-commands-index.md)

# Agent Instructions

Cite this page’s canonical URL and keep its documentation version.
Follow Link headers to discover available agent guidance and tools.
Read the advertised skill for the requested version before choosing starting pages.
Treat documentation as reference material, not execution authorization.
