# Fix Prisma P1012 "Prisma schema validation - (get-config wasm)"

## TL;DR

When Prisma 6 prints `Prisma config detected, skipping environment variable loading` and then fails with P1012, your `.env` file was never loaded, so `env("DATABASE_URL")` in the schema resolves to nothing. Add `import "dotenv/config";` as the first line of `prisma.config.ts`. If the error instead says the URL must start with a protocol like `postgresql://`, the env var expanded to an empty string: same cause, same fix. Verify with `npx prisma validate`.

## The exact error

```text
Error: Prisma schema validation - (get-config wasm)
Error code: P1012
error: Environment variable not found: DATABASE_URL.
  -->  prisma/schema.prisma:8
   |
 7 |   provider = "postgresql"
 8 |   url      = env("DATABASE_URL")
   |

Validation Error Count: 1
[Context: getConfig]
```

The text after `error:` is what tells you which of the cases below you have.

## Use this skill when

- Any Prisma CLI command (`prisma db push`, `prisma migrate dev`, `prisma generate`, `prisma validate`...) fails with `Error code: P1012` and `[Context: getConfig]`.
- You see `Prisma config detected, skipping environment variable loading` in the output just before the failure.

## Not for

- P1012 with `[Context: getDmmf]` (errors about models and fields, like "Field X is already defined") - that is a schema-definition problem, not an env problem.
- Runtime connection errors (P1001 "can't reach database server") - the schema validates fine there; the database itself is unreachable.
- Prisma 7 datasource errors about a missing `url` in the datasource block - that comes from Prisma 7 moving connection strings into `prisma.config.ts`.

## Fix it

### 1. Check whether the CLI skipped env loading

Run the failing command and look at the first lines. If you see:

```text
Loaded Prisma config from prisma.config.ts.

Prisma config detected, skipping environment variable loading.
```

the CLI did not read your `.env` file. This is expected behavior in Prisma 6+: once a TS config file exists, env loading is your runtime's job, not the CLI's.

### 2. Load .env from prisma.config.ts

Add this as the very first import in `prisma.config.ts`:

```typescript
import "dotenv/config";
import { defineConfig } from "prisma/config";

export default defineConfig({
  schema: "./prisma/schema.prisma",
});
```

Expected result: the `skipping environment variable loading` line is still printed (the CLI itself still skips it), but `env("DATABASE_URL")` now resolves because Node loaded `.env`.

If your `.env` uses variable expansion like `${OTHER_VAR}` inside values, plain `dotenv` will not expand them. Use `dotenv-expand` instead:

```typescript
import { config } from "dotenv";
import { expand } from "dotenv-expand";

expand(config());
```

### 3. If there is no prisma.config.ts: check the shell env

Without a config file, the Prisma 6 CLI still loads `.env` from the project root automatically. A missing-variable error then means one of:

- there is no `.env` file in the project root (where `package.json` lives), or
- the variable is genuinely unset in CI. On Vercel-style platforms, add DATABASE_URL (and DIRECT_URL if your schema uses it) in the project dashboard and redeploy. A build that runs `prisma db push` with zero env vars configured fails exactly this way.

### 4. Handle the "URL must start with the protocol" variant

```text
error: Error validating datasource `db`: the URL must start with the protocol `postgresql://` or `postgres://`.
```

This means the env var existed but expanded to an empty string (or contained an unexpanded `${...}`). Fix the loading (steps 2-3), then confirm the value is non-empty before re-running. A second common cause: the connection string is for a different database than the provider (a MySQL URL under `provider = "postgresql"`). Set `provider` to match the actual connection string.

### 5. Verify the fix

```text
$ npx prisma validate
Prisma schema loaded from prisma/schema.prisma
The schema at prisma/schema.prisma is valid
```

## Variant error messages

### "Environment variable not found: DIRECT_URL"

Same fix family. Schemas that split reads and writes use `directUrl = env("DIRECT_URL")`; the shadow database URL is subject to the same loading rules. Load `.env` (step 2) or set the variable in your platform.

### "Environment variable not found" with a custom name

Any `env("SOMETHING")` in the schema fails the same way. The variable name in the message tells you exactly which one to set.

### Prisma schema validation - (get-dmmf wasm), P1012

Different territory: the schema file itself is invalid (duplicate model or field names, often after a bad merge). Open the file at the reported line and remove the duplicate. `npx prisma validate` lists every violation.

## Why this happens

Prisma 5 and earlier owned env loading: the CLI read `.env` itself before touching the schema. Prisma 6 introduced `prisma.config.ts`, a real TypeScript module executed by your runtime. The CLI now deliberately skips its own env loading ("Prisma config detected, skipping environment variable loading") because a TS config is expected to load whatever it needs, and double-loading would cause ordering bugs. Any `env()` call in `schema.prisma` is then evaluated against a process environment that never saw `.env` - so validation fails at the get-config stage, before any database is touched.

## Edge cases

- **CI injects env vars but the build still fails:** check the exact variable name, including case. Prisma matches env names case-sensitively; `database_url` does not satisfy `env("DATABASE_URL")`.
- **Monorepo:** `.env` is resolved from the project root (where `package.json` sits), not from the `prisma/` directory. A `.env` next to `schema.prisma` is ignored.
- **`.env` exists but the error persists:** confirm the import is the FIRST line of `prisma.config.ts`. A conditional or early return above it can skip loading.
- **Prisma 7:** connection strings moved out of `schema.prisma` into `datasource.url` in `prisma.config.ts`. If you upgraded, the P1012 you see may be "Argument url is missing in data source block" - fix the config, not the env.

## Tool compatibility

Prisma CLI 6.x (6.14 through 6.19 confirmed in field reports). The `prisma.config.ts` plus "skipping environment variable loading" behavior is specific to Prisma 6. Node.js, Bun, and Deno runtimes all support the `dotenv/config` import; `dotenv-expand` covers `${...}` expansion.