Generating Automated Multiple Environment.ts In Angular Using .env And Node Js
Setting up Angular for Multiple Environment
- Fresh installation of angular. From terminal type in
ng new <projectName>
- Go to inside the project
cd projectName
- Generate environment files
ng generate environments
- This is the expected directory and files that will be generated
projectName/src/environments
environment.ts
environment.development.ts
environment.staging.ts
- For debugging, import the environment to app.component.ts
import { Component } from '@angular/core';
import { environment } from './../environments/environment';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
constructor() {
console.log(environment.production); // Logs false for development environment
}
title = 'app works!';
}
- Rename some of the files accroding to the needs of the project. Like for instance I renamed it to
projectName/src/environments
environment.ts
environment.staging.ts
environment.prod.ts