Angular Warning: bundle initial exceeded maximum budget

avatar
Borislav Hadzhiev

Last updated: Apr 4, 2024
4 min

banner

# Angular Warning: bundle initial exceeded maximum budget

The Angular "Warning: bundle initial exceeded maximum budget. Budget 500.00 kB was not met by X kB with a total of Y kB." is shown when the size of the maximum budgets configuration property in your angular.json file is exceeded.

To resolve the issue, set the maximumWarning property under budgets to a greater value, e.g. 4mb.

warning bundle initial exceeded maximum budget

Here is the complete stack trace.

shell
Warning: bundle initial exceeded maximum budget. Budget 500.00 kB was not met by 112.57 kB with a total of 612.57 kB. Error: bundle initial exceeded maximum budget. Budget 100.00 kB was not met by 512.57 kB with a total of 612.57 kB.
  1. Open your angular.json file (it is located in your project's root directory, right next to package.json) and look for the budgets configuration property.

  2. Once you find the budgets property, look for the object that has its type property set to initial.

  3. Set the maximumWarning property to 4mb.

  4. Set the maximumError property to 5mb.

angular.json
"budgets": [ { "type": "initial", "maximumWarning": "4mb", "maximumError": "5mb" } ]

increase maximum warning property under budgets

  1. Open your terminal in your project's root directory (where package.json is).

  2. Rerun the following command to build your Angular application for production.

shell
npx ng build --configuration production

build angular app for production

If you get the "Error: bundle initial exceeded maximum budget. Budget X kB was not met by Y kB with a total of Z kB.", then you have to increase the size of the maximumError property.

Find the property maximumError under configurations > production > budgets and increase its value to e.g. 5mb or more (if necessary).

build angular app for production

You might also have to increase the values in the budgets object where type is set to anyComponentStyle if the issue persists.

angular.json
"budgets": [ { "type": "initial", "maximumWarning": "8mb", "maximumError": "9mb" }, { "type": "anyComponentStyle", "maximumWarning": "2mb", "maximumError": "4mb" } ]

The Angular budgets property is used to configure size budgets.

Try to rebuild after increasing the maximum size.

shell
npx ng build --configuration production

As your application grows and you install more third-party packages, its size increases.

The Angular CLI enables you to set a size threshold in your angular.json configuration file.

This way you can ensure that your Angular app stays within the predefined boundaries.

Size budgets can be specified for the entire application and for particular parts.

You can specify the size values in the following formats.

Size ValueDescription
123 or 123bSize in bytes.
123kbSize in kilobytes.
123mbSize in megabytes.
12%Percentage of size relative to baseline. (Not valid for baseline values.)

If you've configured a budget in your angular.json file and the specified size is exceeded, you will get a warning or an error depending on whether the maximumWarning or maximumError property value has been exceeded.

Notice that the type property is set to initial in your first budget object.

budgets type set to initial

The initial value represents the size of the JavaScript that is needed for bootstrapping the Angular application.

By default, the value raises a warning (maximumWarning) at 500kb and an error at 1mb (maximumError).

You might also notice that there is a second configuration object underbudgets where the type property is set to anyComponentStyle.

The anyComponentStyle value represents the size of any one component stylesheet.

By default, the value raises a warning (maximumWarning) at 2kb and an error at 4kb (maximumError).

type property set to anycomponentstyle

  • maximumWarning - when the value is exceeded, the "Warning: bundle initial exceeded maximum budget" is shown.

  • maximumError - when the value is exceeded, the "Error: bundle initial exceeded maximum budget" is raised.

If the warning/error persists, try to set the properties to higher values.

angular.json
"budgets": [ { "type": "initial", "maximumWarning": "8mb", "maximumError": "9mb" }, { "type": "anyComponentStyle", "maximumWarning": "2mb", "maximumError": "4mb" } ]

# Remove unused dependencies from your Angular project

If the issue persists, there might be some large third-party libraries that you aren't using or you can replace them with slimmer libraries.

You can use tools like bundlephobia.com to check the size of a specific package.

visit site for checking package size

Simply type the name of the package into the search field.

check-size-of-package

The size of your application is usually increased when you install and import third-party modules.

Some third-party modules don't focus on size optimizations and rely on too many other third-party modules.

When you npm install or yarn add a module, you are likely installing tens or hundreds of modules under the hood.

Decreasing your reliance on third-party packages is the best way to decrease the bundle size of your Angular application.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.