This guide explains how to configure an Angular workspace using the angular.json file. You’ll learn how to define projects, set build and test options, manage environments, and customize your development server. By the end, you’ll be able to tweak your workspace to fit your project’s needs.
What You’ll Need
- An Angular project created with the Angular CLI (version 6 or later). If you don’t have one, run
ng new my-project. - Basic familiarity with JSON syntax and the command line.
- Node.js and npm installed on your machine.
Step-by-Step
- Locate and open
angular.json. It sits at the root of your Angular workspace. Open it in any text editor. - Understand the top-level structure. The file has a
versionfield, anewProjectRoot(where new projects are created), and aprojectsobject that holds every project in the workspace. There’s also adefaultProjectfield that tells the CLI which project to use when none is specified. - Inspect a project definition. Inside
projects, each project has aprojectType(application or library),root,sourceRoot,prefix(used for component selectors), and anarchitectsection. Thearchitectsection defines targets likebuild,test, andserve. - Configure the build target. Under
architect.build, set thebuilder(e.g.,@angular-devkit/build-angular:browser). Inoptions, you can setoutputPath,index,main,polyfills,tsConfig, andassets. For example, to change the output folder todist/my-app, set"outputPath": "dist/my-app". - Add a development configuration. Under
architect.build.configurations, you’ll see adevelopmentconfiguration. You can add or modify configurations here. For instance, to enable source maps in development, add"sourceMap": trueto that configuration. - Set up production configuration. The
productionconfiguration usually includesoptimization: true,fileReplacements(to swap environment files), andbudgets. You can adjust these to match your performance goals. - Configure the serve target. Under
architect.serve, set thebuilderto@angular-devkit/build-angular:dev-server. Inoptions, you can setbrowserTargetto point to a build target, likemy-app:build. Add configurations fordevelopmentandproductionthat reference the corresponding build configurations. - Customize the test target. For unit tests, the
builderis@angular-devkit/build-angular:karma. Setmainto your test entry file,polyfillsto your test polyfills, andtsConfigto your test tsconfig. You can also setcodeCoveragetotrueto generate coverage reports. - Add or modify file replacements. In the
productionbuild configuration, you’ll often seefileReplacementsthat swapsrc/environments/environment.tswithenvironment.prod.ts. You can add more replacements for other configurations. - Define asset globs. In the
build.options.assetsarray, list files or folders to be copied as-is to the output. For example, add"src/favicon.ico"or"src/assets". - Set up style and script bundles. In
build.options.stylesandscripts, list global CSS/SCSS files and JavaScript files to include in the bundle. For instance, add"src/styles.css"tostyles. - Save and test. After making changes, run
ng serveorng buildto see if everything works. Useng build --configuration productionto test the production build.
Common Mistakes
- Invalid JSON syntax. A missing comma or an extra bracket will break the file. Use a JSON validator or an editor with JSON support.
- Wrong paths. Paths in
angular.jsonare relative to the project root or workspace root, depending on the field. Double-check that your paths exist. - Forgetting to update
fileReplacements. If you add a new environment file, you must add the corresponding replacement in the production configuration, or the wrong file will be used. - Using the wrong builder. Each target has a specific builder. Copying a builder from another target can cause errors.
- Not using configurations. Putting all settings in
optionsinstead of configurations makes it hard to switch between development and production.
Tips
- Use the CLI to generate configurations. Running
ng generate configorng addcan updateangular.jsonautomatically, reducing errors. - Keep the file readable. Use comments? JSON doesn’t support comments, but you can use a separate documentation file or keep the structure clean.
- Leverage
defaultProject. If you have multiple projects, set the default so you don’t have to specify it every time. - Use
ng serve --configuration=productionto preview production behavior locally. - Check the official Angular documentation for the full list of options for each builder.
Troubleshooting
- Build fails with “Invalid configuration”. Check the JSON syntax and ensure all referenced files exist. Use
ng build --verbosefor more details. - Changes to
angular.jsondon’t take effect. Restart the dev server or build process. The CLI reads the file at startup. - Cannot find module ‘x’. Check that the path in
mainorpolyfillsis correct and that the file exists. - Styles not loading. Ensure the style file path is correct and that the file is not empty. Also check for CSS syntax errors.
- Environment variables are undefined. Verify that
fileReplacementsis correctly set and that the replacement file exports the same variables.
Frequently Asked Questions
Can I have multiple projects in one workspace?
Yes, the projects object can contain multiple entries. Each project has its own configuration. Use ng generate application my-app2 to add a new one.
How do I change the port for the dev server?
In the serve target, add "port": 4201 to options. Alternatively, run ng serve --port 4201.
What is the difference between options and configurations?
options are the default settings for the target. configurations are named sets of overrides that you can apply with --configuration. For example, ng build --configuration production merges the production configuration over the defaults.
How do I add a new global stylesheet?
Add the path to the styles array in the build target’s options. For example, "styles": ["src/styles.css", "src/theme.scss"].
Can I use environment variables in angular.json?
No, angular.json is static JSON. For dynamic values, use file replacements or a custom builder/plugin.
Why does my build output go to a different folder?
Check the outputPath in the build target’s options. It is relative to the workspace root. Also, ensure you are not overriding it with a configuration.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Valheim Server Not Showing in Community List Fixes
- DisplayPort vs HDMI: Which Cable Should You Use?
- Soundbar vs 2.1 Speaker System: Which Is Better?
- Survive Lava for Brainrot Tier List170 points
- MySQL Dump Import: Fix Common Errors
- Mouse Moving on Its Own? Causes & Fixes