Loading Environment Variables with `dotenvx`
2025-03-14
I was never a fan of using dotenv
to load environment variables. My main problem is having to mix “development” logic with application code. Ideally my application should not care at all about dealing with its environment, it should come from the outside.
Looking at the docs for dotenv
(because something wasn’t working as I expected, even though the library does literally one thing, probably PEBKAC) I noticed that the creator wrote a new library called dotenvx
(I preferred Twittenv) and it solves this problem in a very neat way. With dotenvx
I can just change the way I call my scripts, without touching the application code:
// package.json
{
"scripts": {
"db:push:dev": "dotenvx run -f .env.dev -- drizzle-kit push",
"db:push:prod": "dotenvx run -f .env.prod -- drizzle-kit push"
}
}
I like this approach a lot, now dotenvx
is a true development dependency and the boundary for environment variables in my application code is process.env
, as it should be.