An open API service indexing awesome lists of open source software.

https://github.com/ajsalemo/angular-env-var-with-processenv

An Angular application that shows how to grab environment variables with process.env.
https://github.com/ajsalemo/angular-env-var-with-processenv

Last synced: 4 months ago
JSON representation

An Angular application that shows how to grab environment variables with process.env.

Awesome Lists containing this project

README

          

# angular-environment-variables-with-processenv

This repo shows how to utilize `process.env.YOUR_ENV_VARS` with Angular.

By default, Angular uses the `src/environments/environment.prod.ts` or `src/environments/environment.ts` folder for environment variables - and assumes you will be hardcoding property and values directly within. This differs from other SPAs where you can pass them in via `process.env.` and their respective prefix to expose them to the browser.

The main points to review are:
- `package.json`:

```json
"scripts": {
"ng": "ng",
"config": "ts-node ./scripts/setenv.ts",
"dev": "npm run config -- --environment=dev && ng serve",
"build": "npm run config -- --environment=prod && ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
}
```

Note the "config" script which is being ran in "build". We use `yargs` to set the environment here. Change this as needed (eg., dev, uat, prod, etc.)

- `scripts/setenv.ts`:

This file picks up the environment we specify (above) through `process.env.` and writes out whatever environment variable's we want to the `src/environments/` file. This picks up