Skip to main content

Environment Variables

Environment variables help us define static variables for our application. They allow us to configure our project for different deployments without changing the code inside our application. We can define environment variables for production, development, and even custom environment variables.

When we create our application, two available environment variables are provided for us. If we check the src/environments directory, we can see that there are environment.ts and files.

Using Environment Variables in a Project

To use our environment variables inside our project, we can simply import the src/environment/environment.ts file. The CLI will be responsible for replacing the value of the environment variables depending on the command.

Example if we have two environment variables:

Development environment variables

filename: src/environments/environment.ts

export const environment = {
hostURL: ''

Production environment variables

filename: src/environments/

export const environment = {
hostURL: ''

We just need to import the development environment variables into our application. To use the production environment variable we can build the application using the following command:


Never import other environment variables in the application other than the development environment variables.

mn build --env prod

or to serve it:

mn serve --env prod

Custom Environment Variables

To create a new environment variable, we just need to create a file named environment.<env name>.ts, where <env name> is the name used in the CLI option --env, as shown in the following example:

Example if we have environment.stage.ts:

mn serve --env stage

Using Environment Variables in CLI

To use our environment variables in the CLI command, we just need to pass the environment name to the --env option in CLI commands.

Example, if we have environment variables like the following:

├── environment.ts
├── environment.uat.ts

And we want to build our application using the uat environment. Then we can use the following command:

mn build --env uat