Skip to content

Commit 078593e

Browse files
authored
docs(core): env var configuration files (#27163)
Fixes #26653
1 parent 3dbbfd7 commit 078593e

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

docs/shared/guides/define-environment-variables.md

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Environment variables are useful to store system-wide values such as the directo
55
(PATH), OS version, Network Information, and custom variables. These env variables are passed at build time and used at
66
the runtime of an app.
77

8-
## Setting environment variables
8+
## Set Environment Variables
99

1010
By default, Nx will load any environment variables you place in the following files:
1111

@@ -53,15 +53,38 @@ We recommend nesting your **app** specific `env` files in `apps/your-app`, and c
5353
for workspace-specific settings (like the [Nx Cloud token](/ci/recipes/security/access-tokens)).
5454
{% /callout %}
5555

56-
### Pointing to custom env files
56+
### Environment Variables for Configurations
57+
58+
Nx will only load environment variable files for a particular configuration if that configuration is defined for a task, even if you specify that configuration name from the command line. So if there is no `development` configuration defined for the `app`'s `build` task, the following command will use `.env.build` instead of `.env.build.development`:
59+
60+
```shell
61+
nx build app --development
62+
```
63+
64+
In order to have Nx actually use the `.env.build.development` environment variables, the `development` configuration needs to be set for the task (even if it is empty).
65+
66+
```jsonc {% fileName="apps/app/project.json" highlightLines=["5-7"] %}
67+
{
68+
"targets": {
69+
"build": {
70+
// ...
71+
"configurations": {
72+
"development": {}
73+
}
74+
}
75+
}
76+
}
77+
```
78+
79+
### Point to Custom Env Files
5780

5881
If you want to load variables from `env` files other than the ones listed above:
5982

6083
1. Use the [env-cmd](https://www.npmjs.com/package/env-cmd) package: `env-cmd -f .qa.env nx serve`
6184
2. Use [dotenvx](https://github.com/dotenvx/dotenvx): `dotenvx run --env-file=.qa.env -- nx serve`
6285
3. Use the `envFile` option of the [run-commands](/nx-api/nx/executors/run-commands#envfile) builder and execute your command inside of the builder
6386

64-
### Ad-hoc variables
87+
### Ad-hoc Variables
6588

6689
You can also define environment variables in an ad-hoc manner using support from your OS and shell.
6790

0 commit comments

Comments
 (0)