Parse .env files in code with loadEnvFile
process.loadEnvFile() loads a .env file into process.env with no dotenv dependency, and util.parseEnv() returns the values as an object instead.
A seed script needs DATABASE_URL from the project’s .env file. Call loadEnvFile() in the script to use the same setup wherever you run it.
Load the file from code
loadEnvFile from node:process reads a file and copies its keys into process.env. With no argument it reads ./.env from the process’s working directory. Copy the three settings below into that file, then run the JavaScript in load.mjs with those variables unset.
// .env
// PORT=4300
// DATABASE_URL="postgres://localhost:5432/app"
// GREETING='hello # not a comment'
import { loadEnvFile } from 'node:process'
loadEnvFile()
console.log(process.env.PORT, typeof process.env.PORT)
console.log(process.env.DATABASE_URL)
console.log(process.env.GREETING)
// 4300 string
// postgres://localhost:5432/app
// hello # not a commentThe quotes are gone, the # inside quotes survives, and every value is a string, so convert PORT with Number() yourself.
It arrived in Node v21.7.0 and v20.12.0 and became stable in v24.10.0 and v22.21.0. To load a different file, pass its path to loadEnvFile('.env.test').
Values already in the environment win
Run PORT=9000 node load.mjs and the script prints 9000. The file fills in what is missing and never overwrites a variable the shell, the container or the CI runner already set, which is what you want in production.
A missing file throws Error: ENOENT: no such file or directory, open '.env.local'. For a file that only exists on some machines, catch that code and let anything else through: try { loadEnvFile('.env.local') } catch (err) { if (err.code !== 'ENOENT') throw err }.
Call it before any import reads the environment
A static import of the config module runs its top-level code before the body of main.mjs. With PORT unset, save these as two files and run node main.mjs.
// config.mjs
export const port = process.env.PORT ?? 'unset'
// main.mjs
import { loadEnvFile } from 'node:process'
import { port } from './config.mjs'
loadEnvFile()
console.log('config.mjs saw', port, '| process.env has', process.env.PORT)
// config.mjs saw unset | process.env has 4300process.env has the value, but config.mjs read it too early. Move the call into its own file, env.mjs, containing the import and loadEnvFile(), and make import './env.mjs' the first line of main.mjs. In this example, env.mjs finishes before config.mjs reads process.env.PORT. The config then sees 4300.
Parse without touching process.env
Sometimes you want the values as an object, for a test or for a child process, without changing the current environment. parseEnv from node:util takes the file’s text and returns one. Run this example in a separate Node process with PORT unset.
import { readFileSync } from 'node:fs'
import { parseEnv } from 'node:util'
const config = parseEnv(readFileSync('.env', 'utf8'))
console.log(config.PORT, config.DATABASE_URL)
console.log(process.env.PORT)
// 4300 postgres://localhost:5432/app
// undefinedprocess.env.PORT stays undefined, and if a key appears twice the util docs show the last one winning. To hand the values to a child process, spread them into its environment: spawnSync('node', args, { env: { ...process.env, ...config } }). The child sees PORT as 4300 while the parent's stays unset.