How can arguments be passed via the CLI when constructing an Angular application?

In my current Angular 16 project, I am facing a unique scenario where each machine running the application locally has a different IP address. This means that the server URL will vary for each instance. Currently, I am retrieving this URL from the environment.ts file as follows:

export const environment = {
    apiUrl: 'http://11.22.33.44:5678/',
};

Every time I need to build the application, I have to manually change the URL. What I aim to achieve is a CLI command method for this purpose e.g.

ng build --configuration=production --serverUrl=http://55.66.77.88:5678/
.

Below is a snippet of my angular.json configuration for the production environment:

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

I came across an article on Stack Overflow here, which suggests creating multiple environments but it doesn't provide a fully dynamic solution.

Answer №1

To begin, start by assigning a temporary value to the apiUrl parameter:

export const environment = {
    apiUrl: 'TEMPORARY_VALUE',
};

Next, during the project build process, utilize the --env command to define the actual value of the apiUrl environment variable, for example:

ng build --configuration=production --env.apiUrl='YOUR_ACTUAL_API_HERE'

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

Include asterisk symbol at the end of a web address following a designated keyword

I currently have a URL structured in the following way: https://localhost:8080/user/login But, there is an option to manually add query parameters which could result in a URL like this. https://localhost:8080/user/login?ten=123456 This leads me to seek ...

Getting exported members through Typescript Compiler API can be achieved by following these steps:

I am currently working on a project hosted at https://github.com/GooGee/Code-Builder This particular file is being loaded by the Typescript Compiler API: import * as fs from 'fs' Below is a snippet of my code: function getExportList(node: t ...

Having trouble compiling the Electron App because of a parser error

Struggling to set up a basic electron app using Vue 3 and Typescript. Following the successful execution of certain commands: vue create app_name cd .\app_name\ vue add electron-builder npm run electron:serve Encountering issues when trying to i ...

Performing a search through a string array and updating a specific portion of each string

I am faced with an array containing a long list of Strings, and my goal is to filter out all the strings that begin with 'INSERT ALL' and replace the number inside the parentheses with the string ' NULL' Here is the original array: le ...

What could be causing the Timeout error to occur in my Jasmine unit test for the HTTP interceptor?

I've been facing challenges with unit testing my interceptor since yesterday afternoon. Despite following multiple tutorials and Stack Overflow answers, I have managed to write what seems like the best code so far. However, I'm encountering an er ...

Angular 2 - Error: Regular expression missing forward slash syntax

Recently, I began working on an Angular 2 tutorial app using this repository. While I can successfully launch the app and display static content, I am facing challenges with rendering dynamic content from the component. I have a feeling that the error migh ...

The FullCalendarModule does not have a property named 'registerPlugins' in its type definition

Currently in the process of integrating fullcalendar into my Angular 15 project and encountering the following error: Error: src/app/views/pages/takvim/takvim.module.ts:18:20 - error TS2339: Property 'registerPlugins' does not exist on type &apo ...

Error in Angular 6: Unable to access property 'mapName' as it is undefined

For my web project using the Mean Stack with Angular 6, I need to create a form that includes an uploaded file. Below is the HTML code snippet: <ng-template #content let-c="close" let-d="dismiss"> <div class="modal-header"> <h4 class= ...

Tips for displaying a collection of items received from an asynchronous API request

I'm having a little trouble with an Angular (Javascript) issue. I am trying to retrieve a list of roles such as Admin, Viewer, and Guest from an API and display them on the user interface. Here are the things I've attempted: TS: ngOnInit() { ...

Using Protractor to wait for an asynchronous service call within the OnInit method of a component in order to test a specific value

I'm currently encountering an issue with Protractor while testing a Hello World service in my Angular2+ application that provides a value for an H1 tag. When I insert plain text into the H1 tag, I am able to test it successfully. However, when I use a ...

including ngb-pagination does not cause the page to load

After adding the ngb-pagination documentation from this link, the page fails to load properly. <ngb-pagination [(page)]="page" [pageSize]="pageSize" [collectionSize]="items.length"></ngb-pagination> Below is ...

Title positioned between two buttons in the header

I am trying to add two menu buttons in my header, but the Hamburger menu button is not aligning to the left as expected. Currently, it looks like this: https://i.stack.imgur.com/s5ptT.png Below is the code snippet I am using: <ion-header> <io ...

Having trouble uploading SharePoint list item with attachment from Angular to ASP.NET Core Web API via NgModel

Recently, I successfully added a SharePoint list item with an attachment from Angular to ASP.NET Core Web API. This was achieved using FormControl in the Angular application. I found guidance on how to upload files from Angular to ASP.NET Core Web API at ...

Troubleshooting in React: Addressing the issue of missing property 'children' in type '{ }' while it is required in type 'Props'

I am working with two components, card and cardList. My goal is to include the cardList component in the App component to display a list of fetched data in cards. However, I am struggling with connecting these two components in order to correctly display ...

Strategies for importing a page property into a data service using Angular

Can someone guide me on importing the trackingId from tracking.page.ts to geolocation.service.ts in my Ionic App using Angular and TypeScript? Take a look at a snippet of the code below. tracking.page.ts import { Component, OnInit, ViewChild, ElementRef ...

Eslint is unable to locate file paths when it comes to Solid.js tsx extensions

Whenever I attempt to import TSX components (without the extension), Eslint raises an error stating that it's unable to resolve the path: https://i.sstatic.net/NiJyU.png However, if I add the TSX extension, it then prompts me to remove it: https:// ...

Navigating with hashtags in Angular2 and anchors does not change the page position

I recently came across a helpful post that showed me how to append a fragment to the URL. Angular2 Routing with Hashtag to page anchor Although the fragment is successfully added, I'm encountering an issue where the page does not scroll to the speci ...

Utilizing both Typescript and javascript in a unified project

Recently, I've been working on a NodeJS project and wanted to incorporate TypeScript files. In my initial attempt, I created a TypeScript file with the following content: utilts.ts : export const delimitify = ( strings:Array<string>, delimiter: ...

Error TS2769: Extending styles with props on base style in React Styled-components TypeScript results in a compilation error

I've been attempting to expand a base style to other styled components and encountering a TypeScript error. While I could achieve this in pure JavaScript, I'm facing difficulties in TypeScript. Base file _Modal.ts import styled from 'styled ...

Steps for developing a Global service in Angular

I've come across many discussions on global angular services, but none seem to truly embody the concept of "global". I'm curious if it's feasible to develop a service that doesn't require importing everywhere it's used (i.e. truly ...