0
Failed to load config file "F:\\Documents\\Desktop\\coding projects\\Shopify\\my-turborepo\\packages\\product-db" as a TypeScript/JavaScript module. Error: PrismaConfigEnvError: Missing required environment variable: DATABASE_URL

package.json

{
  "name": "@repo/product-db",
  "version": "0.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "db:generate": "prisma generate",
    "db:migrate": "prisma migrate dev --skip-generate",
    "db:deploy": "prisma migrate deploy"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "dotenv-cli": "^11.0.0",
    "prisma": "^6.19.0"
  },
  "dependencies": {
    "@prisma/client": "^6.19.0"
  }
}

.env file

DATABASE_URL="postgresql://admin:123456@localhost:5432/products?schema=public"

schema.prisma file

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

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model Product {
  id               Int      @id @default(autoincrement())
  name             String
  shortDescription String
  description      String
  price            Int
  sizes            String[]
  colors           String[]
  images           Json
  createdAt        DateTime @default(now())
  updatedAt        DateTime @updatedAt
  categorySlug     String
  category         Category @relation(fields: [categorySlug], references: [slug])
}

model Category {
  id       Int       @id @default(autoincrement())
  name     String
  slug     String    @unique
  products Product[]
}

What is the solution for this?

5 Answers 5

0

Ensure .env is in the correct place, make sure your .env file is located here :

my-turborepo/
  packages/
    product-db/
      .env
      schema.prisma
      package.json

Use dotenv-cli in your Prisma scripts :

Update your package.json scripts inside packages/product-db/package.json like this:

"scripts": {
  "db:generate": "dotenv -e .env -- prisma generate",
  "db:migrate": "dotenv -e .env -- prisma migrate dev --skip-generate",
  "db:deploy": "dotenv -e .env -- prisma migrate deploy"
}

Run from inside the product-db package :

When running commands, cd into the package first:

cd packages/product-db
npm run db:generate

If you run Prisma from the monorepo root, Prisma may look for .env at the root, not in the product-db folder

Sign up to request clarification or add additional context in comments.

Comments

0
  1. Install dotenv
npm i dotenv
  1. Navigate to your prisma.config.ts file in your project. Add this line at the top:
import "dotenv/config";
  1. Run the following:
npx prisma generate

Comments

0

if you have .env file and it's located in the correct place, than you can try one more option without installing dotenv.
just remove prisma.config.ts (in case you don't need it):

When using prisma.config.ts, environment variables from .env files are not automatically loaded.

link: Prisma documentation

Comments

0

TRY THIS METHOD, I HOPE IT WILL WORK

In older versions of Prisma, e.g, "^ 6.15.0". The Environment variables load automatically.

However, in the version you are using, Prisma "^6.19.0" in its ".env" file clearly states that Environment variables in this file will not be automatically loaded.

Please add import "dotenv/config"; to your prisma.config.ts

(For reference, I'm also adding the snapshot of the .env file instructions by Prisma at the end.)

# Environment variables declared in this file are NOT automatically loaded by Prisma.
# Please add `import "dotenv/config";` to your `prisma.config.ts` file, or use the Prisma CLI with Bun
# to load environment variables from .env files: https://pris.ly/prisma-config-env-vars.

2 Comments

It worked Thankyou
Good to know Piyush , that it worked for you. Kindly accept my answer for your question so that other people can quickly find out the correct answer for the question.
-1

If your environment (.env) isn’t loading, check the file path, ensure it’s in the project root, and restart your server.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.