Node 20.6 added built-in .env file loading, so node --env-file=.env index.js does what dotenv used to. It also picks up NODE_OPTIONS from the same file — convenient when you want flags like --experimental-modules colocated with the rest of your runtime config rather than scattered between package.json and shell.
The full post covers the syntax, why this still doesn't help React or React Native (those don't run on the Node runtime in prod), and where Vite/Astro projects already had a similar story for their own dev servers.
Originally published at andreasbergstrom.dev — read the full post there.
Top comments (8)
wj-config: The power of .Net configuration for Node and browser projects
Interesting, though I can't remember any use case where I wished to pull in env vars from all over the place. For backend services I would probably use a config service instead.
The package allows you to fetch configuration. When you say "env vars from all over the place", you are thinking environment variables as the only possible configuration source. Your mind is playing you tricks.
Hehe, no that's why I said config service :). But good look with your package! Perhaps add a section where you compare its features with node-config.
It pretty much does everything node-config can, plus everything dotenv can, plus URL building functions, plus conditional inclusion of data sources. All in half the code. Oh, and works for browser or Node/Deno projects. The only thing I don't provide is a command-line args data source because that's a Node-only thing and the package is meant to work as-is on browsers.
Cool, does it work with NestJS?
Never tried it as I avoid Node for back-end. I always do back-end in C#. However, it seems like one more Node usage to me, so it will most likely work.
If you are coming from C# I think you'd enjoy NestJS as it's heavily inspired by the .Net web alternatives (full Typescript, using DI etc.), and utilizing decorators a lot. I hated Node backends until I found Nest.