Environment variables not registering in Prisma schema

Viewed 28

I am finding the only way the Prisma schema can detect environment variables is to include a .env file in the root directory of its app.

I am using docker compose to build a few services, one of which is an app that manages a database using Prisma. The environmental variables is available in the app via process.env however Prisma can not detect this using the env function.

Here is how I am accessing the variable in schema.prisma:

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

And I am passing the variable in the docker compose in this way:

  app:
    build:
      context: ./app
    environment:
      DATABASE_URL: ${DATABASE_URL}

How can I make this function detect environment variables that don't necessarily belong to a .env variable?

1 Answers

I eventually worked this out! When I am running commands in my terminal I am running them outside of the docker container. So the prisma code running inside docker works perfect, but the commands I run outside can't see that .env file.

I was able to solve by adding the env values to my scripts. It may not be the most elegant solution but it works. For example:

eval $(grep '^DATABASE_URL' ../.env) prisma db pull && prisma generate
Related