Developing and deploying Angular applications utilizing configuration settings

Deploying an Angular app in an Openshift container with multiple environments (Dev, QA, Prod) poses a challenge.

The CLIENT_ID is used in the code base with environment.CLIENT_ID to access its value. It works fine on localhost.

There are two environment files in the source code:

  1. environment.ts
    export const environment = {
       production: false,
       CLIENT_ID: xyz-456
    }
  1. environment.prod.ts
    export const environment = {
       production: false,
       CLIENT_ID: "$CLIENT_ID"
    }

project.json file

"configurations": {
  "production": {
    "fileReplacements": [
      {
        "replace": "src/environments/environment.ts",
        "with": "src/environments/environment.prod.ts"
      }
    ],
}

ocp_configs/qat-a/app_cfg.env file

CLIENT_ID=abcd-123

In the Openshift container under ConfigMaps YAML, QAT values are displayed as CLIENT_ID: abcd-123.

Issue - The production configuration works correctly locally with the nx serve command. However, after deployment for QAT, it still fetches Dev values instead of QAT values.

When accessing the URL for the QAT app, the CLIENT_ID remains as xyz-456, while it should be abcd-123.

Initially, FileReplacement seemed to be the problem, but I couldn't find a solution despite searching. Using Angular 14.

Answer №1

There is a lot of crucial information missing that could assist in finding a solution. One key piece is How exactly is your application being served?

Is the file replacement functionality working?

If it's not functioning properly, it is likely due to incorrect configuration settings.
Make sure to explicitly specify the configuration when executing the command to build your application:
For example: nx run myApp:build:production (there are other ways to run commands in nx and specify the configuration)

Given the limited details on how the app is delivered, my suspicion is that you are simply using a serve command. It should be noted that the default configuration for this command is set to development.

Regarding Environment Variables in Angular

True Environment variables do not function correctly when building Angular with WebPack. So, I am curious as to why you believe they are working locally. More information would be beneficial.

However, there are methods to integrate them with webpack using some tricks. Nx has resources available on this topic.

Newer versions of Angular may utilize esbuild, but it remains unclear if support for Environment Variables exists and how they can be applied.

It must be noted that these variables will be embedded during the application's build process. Therefore, you cannot build once and deploy across multiple environments.

I also came across this recent Reddit post which might provide some insight.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The utilization of bitwise operator or its presence in an Angular template

I was inspired by the power of rxjs, which led me to convert my address into a BehaviorSubject: address = new BehaviorSubject<Address>(null); Initially, the address is set to null until the user selects a location on the map. In my template, I want ...

Defining the type of a React hook object in TypeScript when calling it

Within my code, I have implemented the following custom hook: const count = useDocsCount({ collectionRef: 'notifications', filter: { filterKey: 'seen', operator: '==', filterValue: false } }) ...

Issue encountered while attempting to install dependencies using Stackblitz

I recently attempted to add ng-select to my StackBlitz project by including it in the dependencies section and importing it in app.module.ts. However, I encountered an error. You can view my code here. import { NgModule } from "@angular/core"; import { Br ...

I am having trouble getting ngFor to properly render my accessible data. Whenever I attempt to use it, it ends up breaking my code. Can someone please

When using *ngFor to iterate through data, everything appears to be working fine until attempting to access nested data within an object inside another object. For example: { "tvshow": [ { "id": "value", "time": { "clock": "valu ...

Issue encountered while utilizing an observable object within a FormGroup

I've encountered an issue that I can't seem to resolve. I have a function that is responsible for creating my FormGroup along with the form fields and validators. The validators are custom validators that require a value from an observable, and t ...

Angular 4: Exploring the Contrasts between ngDoCheck and ngAfterViewChecked

Within the context of Angular 2+, it appears that both ngDoCheck and ngAfterViewChecked have a similar purpose. It is noted that ngDoCheck is invoked whenever change detection is activated, which occurs upon a change in the View. According to the documenta ...

How come TypeScript doesn't retain the positions and types of array elements in memory?

I am currently working on creating an array of objects that consist of questions, associated actions to perform (functions), and arguments to supply to the functions. I am facing issues with TypeScript not recognizing the types and arguments, and I would l ...

Exceed the capacity of a React component

Imagine having a React component that can render either a <button>, an <a>, or a React Router <Link> based on different props passed to it. Is it possible to overload this component in order to accept the correct props for each scenario? ...

Building an electron desktop application with Angular 4 integration and Sqlite3 functionality

I am working on an Angular 4 Electron desktop project and I am struggling to implement SQLite. Despite trying various packages and resources, I have not been successful so far. I need assistance in accessing SQLite from app.component.ts. Any tips or guid ...

What is the best way to retrieve a member from a union type?

Here is a simple example to illustrate the problem: // library.d.ts import type { Bar } from './bar.d.ts'; export interface Foo { bar?: string | Bar | boolean; // and many other properties } // bar.d.ts export interface Bar { baz?: number ...

Angular 6's lazy loading feature causing a problem of not being able to match any routes

I'm currently working on implementing lazy loading in my Angular application. Below are the routes I've set up: app-routing.module.ts const routes: Routes = [ { path: '', loadChildren: './customer/customer.module#Custom ...

Can you guide me on implementing AWS SDK interfaces in TypeScript?

Attempting to create an SES TypeScript client using AWS definitions file downloaded from this link My approach so far: /// <reference path="../typings/aws-sdk.d.ts" /> var AWS = require('aws-sdk'); var ses:SES = new AWS.SES(); The error ...

Having trouble compiling after updating Angular CLI

After switching out my computer and updating to the latest version of Angular CLI, I encountered an error in my project that I couldn't find any information on online. ERROR in ./node_modules/@angular/fire/messaging/messaging.js 23:40 Module parse fa ...

What is the method for converting textarea input into an array in Angular?

I am experiencing an issue with my Angular form that has a textarea. I am using a string array for the formControl, and upon checking the email addresses entered upon ngModelChange, I am assigning them to the array. However, when I submit the form, the arr ...

Image with responsive div buttons

I am trying to add a set of buttons on each image in my masonry image gallery, but I'm facing issues with responsiveness. Whenever I resize the screen, some of the buttons disappear instead of adjusting their size and position accordingly. I want th ...

Loop through a collection of elements of a certain kind and selectively transfer only certain items to a different collection of a different kind

When working with typescript, I am faced with the challenge of dealing with two arrays: interface IFirst{ name: string; age: number } interface ISecond { nickName: string; lastName: string; } myFirstArray: IFirst[]; mySecondArray: ISe ...

Tips for avoiding parent div click interference in Angular

Working with Angular8, I have a div containing a routelink along with other components including a checkbox. Here's the structure: <div [routerLink]="['/somewhere', blablabla]"> <!--other components that navigate to the ro ...

Using React's `cloneElement` to make modifications to a child component while maintaining the reference within a functional component

In the past, I had references in my component while rendering, and it was functioning as expected: // props.children is ReactElement<HTMLDivElement>[] const [childRefs] = useState<RefObject<any>[]>(props.children.map(() => createRef()) ...

`Adjusting video frame on canvas`

When I capture video from a user's camera and display it in a 1:1 square, I encounter an issue when trying to draw the picture to a canvas. The image is not drawn in the same location on the canvas. How can I resolve this problem? To see a live examp ...

Angular dropdown option not displaying tick mark

I'm attempting to add a tick mark before a specific default item in the option dropdown of my Angular application. The item in question is named Steven, as shown in this example. How can I make this happen? For some reason, it doesn't seem to be ...