Upgrading to Angular 10 - Fix CommonJS or AMD dependencies can cause optimization bailouts Upgrading to Angular 10 - Fix CommonJS or AMD dependencies can cause optimization bailouts javascript javascript

Upgrading to Angular 10 - Fix CommonJS or AMD dependencies can cause optimization bailouts


When you use a dependency that is packaged with CommonJS, it can result in larger slower applications

Starting with version 10, Angular now warns you when your build pulls in one of these bundles. If you’ve started seeing these warnings for your dependencies, let your dependency know that you’d prefer an ECMAScript module (ESM) bundle.

Here is an official documentation - Configuring CommonJS dependencies

In your angular.json file look for the build object and add

allowedCommonJsDependencies

as shown below -

"build": {  "builder": "@angular-devkit/build-angular:browser",  "options": {     "allowedCommonJsDependencies": [        "rxjs-compat",         ... few more commonjs dependencies ...      ]     ...   }   ...},


try replacing the rxjs imports rxjs/internal/operators with rxjs/operators.

ex:

import { catchError, retry } from 'rxjs/internal/operators';

with

import { catchError, retry } from 'rxjs/operators';


It is recommended that you avoid depending on CommonJS modules in your Angular applications. Depending on CommonJS modules can prevent bundlers and minifiers from optimizing your application, which results in larger bundle sizes. Instead, it is recommended that you use ECMAScript modules in your entire application.Still you don't care about your bundling size,

To disable these warnings, you can add the CommonJS module name to allowedCommonJsDependencies option in the build options located in angular.json file.

"build": {  "builder": "@angular-devkit/build-angular:browser",  "options": {     "allowedCommonJsDependencies": [        "rxjs-compat"     ]     ...   }   ...},

Source