Comprehensive Guide to Configuring Build Output Path in Angular CLI

Nov 24, 2025 · Programming · 11 views · 7.8

Keywords: Angular CLI | Build Output Path | angular.json Configuration

Abstract: This article provides an in-depth exploration of configuring build output paths in Angular CLI, detailing methods to modify the dist folder path through the outDir property in angular.json and the --output-path parameter of ng build command. It analyzes configuration differences across Angular versions, demonstrates implementation with practical code examples, and discusses baseHref settings for subdirectory deployments.

Overview of Build Output Path Configuration in Angular CLI

In Angular project development, configuring the output path for build files is a common and essential requirement. By default, Angular CLI uses the dist folder as the build output directory, but in real-world projects, developers often need to adjust the output path based on deployment environments or project structures.

Detailed Configuration Methods

Angular CLI provides two main approaches to configure build output paths: permanent settings through project configuration files, or temporary overrides through command-line parameters.

Via angular.json Configuration File

In Angular 6 and later versions, project configuration is unified in the angular.json file. To modify the output path, locate the outputPath property in the project configuration:

{
  "projects": {
    "my-app": {
      "architect": {
        "build": {
          "options": {
            "outputPath": "dist/my-app"
          }
        }
      }
    }
  }
}

Change the value of outputPath to the desired path. For example, to output to a production folder in the project root, set it as:

"outputPath": "production"

Via Command-Line Parameters

In addition to configuration files, you can temporarily specify the output path during build using command-line parameters:

ng build --output-path=production
# Or use the shorthand form
ng build -op production

This approach is particularly useful for continuous integration environments or scenarios requiring different build outputs for various environments.

Version Compatibility Notes

It's important to note that in earlier versions of Angular CLI (Angular 5 and before), the configuration file was named .angular-cli.json, and the corresponding configuration property was outDir:

{
  "apps": [
    {
      "outDir": "./location/toYour/dist"
    }
  ]
}

Starting from Angular 6, the configuration file format was restructured, with outDir being replaced by outputPath, and the configuration structure becoming more organized.

Subdirectory Deployment Configuration

When an Angular application needs to be deployed in a subdirectory of a web server, merely modifying the output path is insufficient; the baseHref property must also be configured:

{
  "projects": {
    "my-app": {
      "architect": {
        "build": {
          "options": {
            "outputPath": "dist/my-app",
            "baseHref": "/my-folder/"
          }
        }
      }
    }
  }
}

Or via command-line parameter:

ng build --base-href=/my-folder/

Practical Application Scenarios

In actual development, output path configuration typically relates to specific deployment requirements. For instance, when integrating an Angular application into a .NET project, you might need to direct build output directly to the .NET project's wwwroot directory:

"outputPath": "../MyNetApplication/wwwroot"

However, with certain build tools (like es-build), additional subdirectories (such as browser) might be automatically created under the specified output path, requiring special attention during deployment.

Best Practices Recommendations

To ensure configuration reliability and maintainability, it is recommended to:

By properly configuring build output paths, you can significantly enhance the deployment efficiency and flexibility of Angular projects, providing robust support for application publishing across different environments.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.