How T3Stack env.js actually works?

I'm trying to figure out what kind of black magic that createEnv function does behind the curtains, when I'm running my application. The reason is that I'm having seed.ts at the root of my project, which I'm using to seed the database, by calling tsx seed.ts. Problem is, that if I import env.js into that seeder-file, it does not have env variables available. Same thing when I'm trying to call Encryption.encrypt() method from one of my lib-files, to encrypt some data, and that lib-file imports env.js, I get error that environmental variable are not set. Should I somehow load the .env file manually on tsx call, or what might I be missing?
3 Replies
Juraj98
Juraj984mo ago
This is not black magic of createEnv, but a black magic of Next. Next automatically looks for .env file and loads the contents of that file into process.env. When you're running tsx seed.ts, this file is not loaded. You can use packages such as dotenv or dotenv-cli to load contents of the .env file when using tsx.
Maastonakki
Maastonakki4mo ago
Ah! Once again, how stupid I feel. I did have dotenv on the project, and even imported in the seed file. Drawback was that I didn't have it imported properly. Quick look into documentation and import "dotenv/config"; did the trick.
Neto
Neto4mo ago
bun automatically reads .env file node 20+ with node --env-file=.env some.js
Want results from more Discord servers?
Add your server