Angular Server-Side Configuration

21.0.3 · active · verified Tue Apr 21

angular-server-side-configuration is an Angular CLI builder and native command-line interface tool designed to enable runtime configuration of Angular applications on the server or within Docker containers using environment variables. This addresses the limitation of Angular CLI's default build-time configuration, providing flexibility in Continuous Delivery environments. It works by searching for environment variable usages at build time and then, via a native CLI, inserting these populated variables into the `index.html` file (or replacing a `<!--CONFIG-->` placeholder) on the host serving the bundled Angular files. The current stable version is 21.0.3, with releases generally aligning with major Angular versions, introducing breaking changes primarily for Angular version upgrades and builder refactoring. Its key differentiator is the separation of build-time processing from deployment-time configuration injection, making it suitable for containerized deployments.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up server-side configuration, build an Angular application using the custom builder, and then access the injected environment variables within an Angular component.

ng add angular-server-side-configuration

# When prompted 'Would you like to use the experimental builders for build and serve?', answer 'n'.

# Configure an environment variable in your shell (e.g., in a CI/CD pipeline or Dockerfile)
export NG_APP_API_URL="https://api.example.com/production"

# Build your Angular application with the ngssc builder
ng run your-project-name:ngsscbuild:production

# Example app.component.ts to consume the config
import { Component, OnInit } from '@angular/core';
import { NgsscService } from 'angular-server-side-configuration';

@Component({
  selector: 'app-root',
  template: `<h1>API URL: {{ apiUrl }}</h1>`
})
export class AppComponent implements OnInit {
  apiUrl: string | null = null;

  constructor(private ngsscService: NgsscService) {}

  ngOnInit(): void {
    // Access configuration injected by the server-side process
    this.apiUrl = this.ngsscService.get('API_URL');
    console.log('Resolved API URL:', this.apiUrl);
  }
}

view raw JSON →