The .env
file is a text file that contains environment variables, commonly used store configuration settings for an application. It is written in a key-value pair format similar to a shell script.
Why doesn’t my application see the environment variables in the .env
file?
By default, a shell does not load the .env
file. This means that when you create a .env
file, your application may not be able to access the environment variables defined in it, unless something causes the .env
file to be loaded into your application’s environment.
Some runtimes loads .env files automatically.
Vite loads
.env
files automatically, making them available asimport.meta.env
. As a result, frameworks based on Vite also inherit this behavior, such as Astro and SvelteKit. Do note that for these frameworks, environment variables may be evaluated at build time or runtime1 and behavior can be different between development and production environments, so refer to the documentation for specifics.Glitch, a platform for building web applications, automatically loads
.env
files in a project as a normal environment variable.Docker Compose automatically loads
.env
files for the purpose of interpolation into thedocker-compose.yml
file. Note that this does not make the environment variables available to the containers managed by Docker Compose.More details for Docker Compose
To make the environment variables available to the containers, specify the environment variable names in the
environment
section of thedocker-compose.yml
file. For example:services: myservice: image: myimage environment: - MY_ENV_VAR
Docker Compose will pass on the contents of the
MY_ENV_VAR
environment variable (which may be automatically loaded from the.env
file) to the container.Alternatively, if you want all environment variables from the
.env
file to be passed to the container, you can use theenv_file
directive in thedocker-compose.yml
file.
Some runtimes lets you load a .env file, but it must be explicitly done.
- Node.js has support for
--env-file
since v20.
- Node.js has support for
Program your application to load the .env file.
Run your application through a wrapper that loads the .env file.
dotenvx lets you run arbitrary commands with environment variables loaded from a
.env
file.More details for dotenvx
To run a command with
.env
file loaded:dotenvx run -- command
To specify the
.env
file to load:dotenvx run -f .env.ci -- command
Docker lets you specify the
--env-file
for the container.
Load variables from
.env
file into the shell.direnv is a shell extension that loads (and unloads) environment variables from a
.env
file into the shell when youcd
into (and out of) a directory. More advanced configuration can be done by using a.envrc
file instead. (Nowadays I use mise-en-place instead of direnv.)mise-en-place can automatically load the
.env
file in a directory by configuring it in.mise.toml
.
Footnotes
Build-time environment variables are evaluated when the application is built and compiled/inlined into the final artifact. They are not changeable at runtime; changing these variables requires a rebuild.
Run-time environment variables are evaluated when the application is running. ↩