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
.envfiles 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
.envfiles in a project as a normal environment variable.Docker Compose automatically loads
.envfiles for the purpose of interpolation into thedocker-compose.ymlfile. 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
environmentsection of thedocker-compose.ymlfile. For example:services: myservice: image: myimage environment: - MY_ENV_VARDocker Compose will pass on the contents of the
MY_ENV_VARenvironment variable (which may be automatically loaded from the.envfile) to the container.Alternatively, if you want all environment variables from the
.envfile to be passed to the container, you can use theenv_filedirective in thedocker-compose.ymlfile.
Some runtimes lets you load a .env file, but it must be explicitly done.
- Node.js has support for
--env-filesince 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
.envfile.More details for dotenvx
To run a command with
.envfile loaded:dotenvx run -- commandTo specify the
.envfile to load:dotenvx run -f .env.ci -- commandDocker lets you specify the
--env-filefor the container.
Load variables from
.envfile into the shell.direnv is a shell extension that loads (and unloads) environment variables from a
.envfile into the shell when youcdinto (and out of) a directory. More advanced configuration can be done by using a.envrcfile instead. (Nowadays I use mise-en-place instead of direnv.)mise-en-place can automatically load the
.envfile 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. ↩