|
9 | 9 |
|
10 | 10 | - ✅ Official recommendation in [dotenv documentation](https://www.dotenv.org/docs/frameworks/angular/vercel) 🔥 |
11 | 11 | - ✅ Webpack and ESBuild support 🚀 |
| 12 | +- ✅ Runtime environment variables (Experimental) 🎉 |
12 | 13 | - ✅ Loading priorities of environment variables with Monorepo Support ([Nx](https://nx.dev), [Turbo](https://turbo.build/), etc.) ✨ |
13 | 14 | - ✅ Easy to use, no configuration required |
14 | 15 | - ✅ Up to date with latest Angular versions |
@@ -227,6 +228,27 @@ You can also access the environment variables starting with `NG_APP_` in the `in |
227 | 228 | </html> |
228 | 229 | ``` |
229 | 230 |
|
| 231 | +## Runtime Environment Variables (Experimental) |
| 232 | + |
| 233 | +By default, environment variables are embedded during the build time which means they are static and cannot be changed at runtime. |
| 234 | + |
| 235 | +If you want to change the environment variables at runtime, you can use the `runtime` option in the `angular.json` configuration. |
| 236 | + |
| 237 | +```json |
| 238 | +"ngxEnv": { |
| 239 | + "prefix": "NG_APP_", |
| 240 | + "runtime": true |
| 241 | +} |
| 242 | +``` |
| 243 | + |
| 244 | +When you set the `runtime` option to `true`, a new environment file will be created during the build with the name `ngx-env.js` in the build output directory containing the environment variables that match the prefix. You can change the environment variables at runtime by modifying this file. |
| 245 | + |
| 246 | +```ts |
| 247 | +globalThis._NGX_ENV_ = { |
| 248 | + NG_APP_VERSION: "10.0.0", |
| 249 | +}; |
| 250 | +``` |
| 251 | + |
230 | 252 | # Defining Environment Variables |
231 | 253 |
|
232 | 254 | ## Command Line |
@@ -471,19 +493,15 @@ Usage `process.env` might introduce typing issues depending on whether your work |
471 | 493 | ## Declare your environment variables in the generated `.env.d.ts` file |
472 | 494 |
|
473 | 495 | ```ts |
474 | | -interface ImportMeta { |
475 | | - readonly env: ImportMetaEnv; |
| 496 | +declare interface Env { |
| 497 | + readonly NODE_ENV: string; |
| 498 | + // Replace the following with your own environment variables, for example: |
| 499 | + // readonly NG_APP_VERSION: string; |
| 500 | + [key: string]: any; |
476 | 501 | } |
477 | 502 |
|
478 | | -interface ImportMetaEnv { |
479 | | - /** |
480 | | - * Built-in environment variable. |
481 | | - * @see Docs https://github.com/chihab/dotenv-run/packages/angular#ng_app_env. |
482 | | - */ |
483 | | - readonly NG_APP_ENV: string; |
484 | | - // Add your environment variables below |
485 | | - // readonly NG_APP_API_URL: string; |
486 | | - [key: string]: any; |
| 503 | +declare interface ImportMeta { |
| 504 | + readonly env: Env; |
487 | 505 | } |
488 | 506 | ``` |
489 | 507 |
|
|
0 commit comments