Build Command
bash
g build # production configuration is the default in modern CLI
ng build --configuration development # unminified, with source maps
ng build --base-href /my-app/ # app lives in a sub folder
Output goes to dist/<project>/browser. This folder is what you upload to a host.
What Production Mode Does
- Minifies and tree-shakes code
- Splits code into lazy chunks with hashed file names for caching
- Enables AOT compilation and optimization
- Applies budgets
Environment Files
Modern CLI projects do not include environment files by default. Create them with:
bash
g generate environments
typescript
// src/environments/environment.ts (production values)
export const environment = {
production: true,
apiUrl: 'https://api.myapp.com',
};
// src/environments/environment.development.ts
export const environment = {
production: false,
apiUrl: 'http://localhost:3000',
};
json
"configurations": {
"development": {
"fileReplacements": [
{ "replace": "src/environments/environment.ts", "with": "src/environments/environment.development.ts" }
]
}
}
typescript
import { environment } from '../environments/environment';
this.http.get(`${environment.apiUrl}/users`);
Public values only: Environment values are compiled into the JavaScript. Never put secrets in them.
Runtime Configuration (One Build, Many Environments)
With environment files you need a separate build for each server. A runtime config file lets you build once and change settings by editing a JSON file on the server.
json
// public/config.json
{ "apiUrl": "https://staging-api.myapp.com", "featureX": true }
typescript
export interface AppConfig { apiUrl: string; featureX: boolean; }
export const APP_CONFIG = new InjectionToken<AppConfig>('APP_CONFIG');
// app.config.ts
providers: [
provideAppInitializer(async () => {
const res = await fetch('/config.json');
const cfg = (await res.json()) as AppConfig;
inject(ConfigStore).set(cfg);
}),
]
@Injectable({ providedIn: 'root' })
export class ConfigStore {
private cfg = signal<AppConfig | null>(null);
set(c: AppConfig) { this.cfg.set(c); }
apiUrl = computed(() => this.cfg()!.apiUrl);
}
Base Href
If your site is at https://example.com/app/, set <base href="/app/"> (or use --base-href). Otherwise assets and routes fail to load.
Checks Before Release
- Run
ng lint (if configured) and fix problems
- Run unit tests:
ng test --no-watch
- Run
ng build and watch for budget warnings
- Test the build locally with a static server:
npx http-server dist/my-app/browser -p 8080 --proxy http://localhost:8080?
- Check the app in the browser for console errors
- Verify the version number and changelog
Preview the build: ng serve is for development only. Always test a real production build before deploying; some bugs only appear in minified code.
Conclusion
ng build creates the optimized site in dist. Use environment files for compile-time values, a runtime config.json for build-once deployments and never store secrets in the bundle.
Quick Summary: ng build -> dist/<app>/browser; environments + runtime config; base href.