DEV Community

Cover image for Finally you can skip dotenv in Node
Andreas Bergström
Andreas Bergström

Posted on • Edited on • Originally published at andreasbergstrom.dev

Finally you can skip dotenv in Node

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)

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

wj-config: The power of .Net configuration for Node and browser projects

Collapse
 
andreasbergstrom profile image
Andreas Bergström • Edited

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.

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

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.

Thread Thread
 
andreasbergstrom profile image
Andreas Bergström

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.

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas • Edited

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.

Thread Thread
 
andreasbergstrom profile image
Andreas Bergström

Cool, does it work with NestJS?

Thread Thread
 
webjose profile image
José Pablo Ramírez Vargas

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.

Thread Thread
 
andreasbergstrom profile image
Andreas Bergström

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.