Next.js Discord

Discord Forum

Environment variables not detected in drizzle config file

Answered
Yusuf posted this in #help-forum
Open in Discord
Avatar
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

Avatar
where do you set DATABASE_URL? what is the name and path of the file?
Avatar
.env.local and it's in the top level
the rest of the project is in ./src though
Avatar
then it should work...
Avatar
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
Avatar
can you give me a reproduction repository?
a sample .env.local file in a minimal nextjs project that also faces this problem
Avatar
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?
ok sure give me a few minutes
Avatar
yes you can console.log it in any server files. for example console.log(process.env.DATABASE_URL) in next.config.js
Avatar
hmm very weird
Avatar
Chien Français Blanc et Noir
If you want to access from client component, try like this process.env.NEXT_PUBLIC_DATABASE_URL
Avatar
you absolutely never want to make your database url public
Avatar
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
Avatar
wdym by hardcoded URL?
Avatar
this works
Image
and this doesn't
Image
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
Avatar
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
Avatar
okay, thanks for the help as always!
I'll keep this open for now
Avatar
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
Image
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
Avatar
interesting
but the push command needs something in the config
Image
Avatar
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
Avatar
still nothing :sad_cry:
Avatar
uh then now i'm completely clueless :sadge: sorry and good luck
Avatar
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
Avatar
FIXED
Avatar
for future reference you need to npm install dotenv and require 'dotenv/config' at the top of the drizzle config
Answer