Fixing "Error: Unknown argument: prod" in Angular Build Commands
Problem Statement
If you're encountering the error "Error: Unknown argument: prod" when trying to build your Angular project with the command ng build --prod --aot
, this is due to a significant change in the Angular CLI. The --prod
flag, which was commonly used for production builds, has been deprecated and removed in recent Angular versions.
This error typically occurs when:
- You've updated your Angular project to version 12+ (deprecated) or 14+ (removed)
- You're using build scripts or CI/CD pipelines that still reference the old
--prod
flag - Your development environment (
ng serve
) works correctly, but production builds fail
Solution
Use --configuration production Instead
The correct approach for Angular 12+ is to replace --prod
with --configuration production
:
ng build --configuration production --aot
Alternatively, you can use the shorthand version:
ng build -c production --aot
INFO
Since Angular 15, ng build
without any configuration flags behaves identically to ng build --configuration production
, making the explicit flag optional in many cases.
Update Package.json Scripts
If you have build scripts in your package.json
file, update them to use the new syntax:
{
"scripts": {
"build": "ng build",
"build-prod": "ng build --configuration production",
"build:ssr": "ng build --configuration production && ng run app:server:production"
}
}
After updating, you can run:
npm run build-prod
Update CI/CD Pipelines and Configuration Files
For projects integrated with .NET or other frameworks using build configuration files, you'll need to update these files:
Before (old syntax in .csproj files):
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --prod" Condition=" '$(BuildServerSideRenderer)' == 'true' " />
After (new syntax):
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --configuration production" />
<Exec WorkingDirectory="$(SpaRoot)" Command="npm run build:ssr -- --configuration production" Condition=" '$(BuildServerSideRenderer)' == 'true' " />
Background: Why This Change Occurred
The Angular team deprecated the --prod
flag in Angular 12 and completely removed it in Angular 14 as part of their effort to streamline the CLI and align with more explicit configuration patterns. This change provides:
- Better consistency across different environments
- More flexibility for custom configurations
- Clearer syntax that explicitly indicates the configuration being used
WARNING
If you're working with multiple environments (development, staging, production), ensure your angular.json
file contains the appropriate configuration definitions for each environment.
Verify Your Angular Version
To check which version of Angular you're using:
ng version
This will help you confirm whether your project has been updated to a version where --prod
is no longer supported.
Alternative Solution for Legacy Projects
If you need to maintain compatibility with both old and new Angular versions temporarily, you can create custom scripts that check the Angular version and use the appropriate command:
#!/bin/bash
ANGULAR_VERSION=$(ng version | grep "Angular CLI" | awk '{print $3}')
if [[ $ANGULAR_VERSION == 1[2-9].* ]] || [[ $ANGULAR_VERSION == 2*.* ]]; then
ng build --configuration production --aot
else
ng build --prod --aot
fi
Conclusion
The "Unknown argument: prod" error is a straightforward fix that requires updating your build commands to use --configuration production
instead of the deprecated --prod
flag. This change affects not only your local development commands but also any CI/CD pipelines, deployment scripts, or configuration files that reference the old syntax.
By making these updates, you'll ensure your Angular projects build successfully across all current and future Angular versions while following the framework's recommended best practices.