How to access .env files natively with Node.js
Ever thought about loading your environment variables directly in Node.js? This tutorial for version 20.6 shows you how!

Editorial translation by Impulso.
People had been waiting years for this Node.js feature, and it finally arrived in October 2023. Node.js now has native support for environment files, also known as dotenvs or env-config files. If you’re new to this, just imagine loading your environment variables directly in Node.js without having to import and export them over and over. That’s exactly what version 20.6 brought us!
The dotenv package
One of the most widely used packages in the developer community is dotenv. It does something incredibly simple but essential: it loads environment variables into the runtime’s memory. If you’re unfamiliar with them, they’re special variables that hold sensitive data that can’t be exposed in the code and are only available in the environment where they’re set. That could be a VM or a serverless function, for example.
If you’d like to read more about these variations, take a look at this article, which also shows the old way of loading them, with dotenv. In fact, it’s interesting to see how far Node has come since then. Essentially, environment variables are ideal for storing database usernames and passwords, or even token secrets. After all, the application needs access to the same machine and host to retrieve that data. And we know that’s much harder than getting access to the code alone.
Ultimately, these variables are stored in what we call dotenv or.env. These files use the INI or bash format, depending on the runtime you’re using (for dotenv, that would be INI). So an environment file might look like this:
SENHA=123
USUARIO=123
Until now, we’ve always needed a module to load these variables, but that’s changed thanks to the arrival of Deno.
Deno
Deno is a JavaScript runtime developed by the creator of Node.js, and it changed how runtimes handle these files. After all, it lets you load them directly using its standard library, so we can use what’s built into the runtime instead of installing a separate module:
// app.ts
import { load } from "https://deno.land/std@0.203.0/dotenv/mod.ts";
Node 20
Node 20.6 introduces a fundamental change that goes even further than what Deno has already done: you can now read .env files directly from the runtime. That saves us the trouble of doing this:
require('dotenv').config()
const {DB_HOST, DB_PORT, DB_USER, DB_PASS} = process.env
const db = require('alguma-lib-de-banco-de-dados')
We can simply use the command node --env-file=<caminho> index.js. That loads all the variables from the file at the specified path into our process.env. For example, imagine a.env file that looks like this:
DB_PASS=pass
DB_USER=root
DB_PORT=5432
DB_HOST=localhost
When we run Node with the command node --env-file=.env index.js, we can use the variable process.env.DB_PASS. It’ll have the value pass, and the other variables will also be set and ready to use in our application. But that’s not the only new feature.
Node Options
Node.js lets you create an environment variable called NODE_OPTIONS. You can put all the options you’d normally pass to Node on the command line directly in that variable. So if we want to start Node with the inspector enabled, for example (using the --inspect flag), we can use: NODE_OPTIONS='--inspect' and now every invocation of node index.js will behave as if it were written node --inspect index.js
There used to be a way to keep these settings in package.json, but support for environment files changes everything. Now you can put your Node options (except, of course, --env-file itself) in an environment variable:
NODE_OPTIONS='--inspect --loader=tsx'
As I showed in my article on TSX, we can load TypeScript files natively using a loader, and we can also load our environment file directly. So with the command above, we can run node --env-file=.env index.ts, for example.
Want to keep up with what’s new in the developer world?
Then you should check out the Impulso community on Discord, where they host tech events and have several channels for talking about software development. There’s room for everyone, whatever your stack!
About the author
Lucas Santos is an Impulser Expert and has been a software engineer since 2011. He’s passionate about technology, knowledge, and content. Follow his work here.


