Next.js Discord

Discord Forum

Environment variables not detected in drizzle config file

Answered
Yusuf posted this in #help-forum
Open in Discord
I've been debugging errors with T3-env not detecting environment variables but it turns out that environment variables are not working at all.

I figured out it was a problem with either next or nodejs when I tried to use process.env.DATABASE_URL in the drizzleorm config and drizzle errored out

I am using the pages/ directory
Answered by Yusuf
for future reference you need to npm install dotenv and require 'dotenv/config' at the top of the drizzle config
View full answer

46 Replies

@joulev where do you set `DATABASE_URL`? what is the name and path of the file?
.env.local and it's in the top level
the rest of the project is in ./src though
then it should work...
neither t3-env nor drizzle were able to detect the variable
and it's just regular process.env nothing fancy
maybe something is wrong with my nodejs configuration
can you give me a reproduction repository?
a sample .env.local file in a minimal nextjs project that also faces this problem
@joulev then it should work...
okay, let's try to debug this, is there a way I can console log the process.env.DATABASE_URL to make sure it's not there?
@joulev can you give me a reproduction repository?
ok sure give me a few minutes
yes you can console.log it in any server files. for example console.log(process.env.DATABASE_URL) in next.config.js
hmm very weird
Chien Français Blanc et Noir
If you want to access from client component, try like this process.env.NEXT_PUBLIC_DATABASE_URL
you absolutely never want to make your database url public
@joulev yes you can console.log it in any server files. for example console.log(process.env.DATABASE_URL) in next.config.js
so something very weird is happening:

- console logging the env variable works
- but when I try to use it in drizzle, it doesn't, I verified this by first using process.env.DATABASE_URL as the connectionURL which worked and then I switched to the hardcoded URL which didn't work
@joulev wdym by hardcoded URL?
this works
and this doesn't
this would make you think that the DATABASE_URL is not working but then again, when running console.log(process.env.DATABASE_URL) it returns the proper thing
@Yusuf and this doesn't
in this case then i'm honestly not sure at all since i've never used drizzle
you have to wait for someone knowing drizzle to help
also i suggest asking in the drizzle discord server
there is a link in their website
okay, thanks for the help as always!
I'll keep this open for now
btw i looked at how they handle connection strings in drizzle and apparently you don't have to use config files? look at this for example https://orm.drizzle.team/docs/installation-and-db-connection/postgresql/postgresjs
so you can just make a db.ts and use env.DB_URL there, then export the db object and use it anywhere
in drizzle config idk but in a db.ts then env.DB_URL definitely works
interesting
oh uhhh then idk at all, so can't help here. good luck!
WAITTTTTT
rename your file to .env
many libraries don't detect the .env.local file
but do detect .env
prisma is an example, it looks like drizzle is the same
here you run the drizzle CLI that ignores .env.local so you have to name it .env
only the nextjs CLI takes note of .env.local
still nothing :sad_cry:
uh then now i'm completely clueless :sadge: sorry and good luck
@joulev uh then now i'm completely clueless <:sadge:1019976768322543727> sorry and good luck
I fucking hate when this happens: in my mind I say I'll do a fun little project for the weekend and than I am in debugging hell
thanks anyways you've helped me plenty of times
FIXED
for future reference you need to npm install dotenv and require 'dotenv/config' at the top of the drizzle config
Answer