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

11

190

I am trying to upgrade my Angular 9 app to Angular 10 version, but I am getting the below warning after the upgrade

rxjs\BehaviorSubject.js depends on rxjs-compat/BehaviorSubject

How can I fix this?

Illusion answered 26/6, 2020 at 10:34 Comment(3)
Can you check if you once did not import from rxjs instead from rxjs/behaviorsubject.Florencio
@JonathanStellwag I have imported it like this - import { BehaviorSubject } from 'rxjs' and everything works fine for Angular 9. But for Angular 10 it does not. I am facing the same issue for map operator as well - It says WARNING in ..\node_modules\rxjs\operators\map.js depends on rxjs-compat/operators/map. CommonJS or AMD dependencies can cause optimization bailouts.Illusion
Does this answer your question? Angular 10 Upgrade - Fix CommonJS or AMD dependencies can cause optimization bailoutsTerence
C
202

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 ... 
     ]
     ...
   }
   ...
},
Crisper answered 26/6, 2020 at 23:6 Comment(9)
I can't find the key word to suprress on my case: WARNING in /home/leonardo/iq/web-iqbot/node_modules/@firebase/app/dist/index.cjs.js depends on '@firebase/component'. CommonJS or AMD dependencies can cause optimization bailouts. For more info see: https://angular.io/guide/build#configuring-commonjs-dependenciesRevelationist
@LeonardoRick Try putting something like this for firebase dependencies: "allowedCommonJsDependencies": [ "firebase", "@firebase/app", "firebase/app", "@firebase/functions", "@firebase/performance", "@firebase/remote-config", "@firebase/component" ], In your case just add '@firebase/component' keyword in the existing list and it should work.Crisper
It worked! Thank you. In my case it was multiple warnings inside @firebase and It only dissapeared when I added all of them as allowed. I was trying to make them dissapear one by oneRevelationist
@GunjanKhanwilkar any chance you improve your answer explaining clearly why packaging with CommonJS can result in larger slower applications?Eslinger
@Eslinger I have updated the link in my answer! You could find a good explanation here - web.dev/commonjs-larger-bundles Cheers!Crisper
For anyone who searches for the file where it should be added - angular.json in the root directory of your project.Kith
@MaximGeorgievskiy Good catch newbies might have difficulties to locate the file.. I have updated the answer for the same thanks!Crisper
worked angular@12Abject
works like a charm for sweetalert2 and moment on Angular 14 🙏Hypnoanalysis
F
114

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

Example:

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

with

import { catchError, retry } from 'rxjs/operators';
Floydflss answered 14/7, 2020 at 12:18 Comment(0)
H
63

It is recommended that you avoid depending on CommonJS modules in your Angular applications. Depending on the CommonJS modules, they 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, if 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 the angular.json file.

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

Source

Herc answered 26/6, 2020 at 11:22 Comment(6)
Thanks for answering. I would still like to know if there are any ECMAScript modules that are recommended as it's substitute? instead of disabling the warnings.Illusion
This did not get rid of the warning messages for me.Twenty
Same for me, I am using lodash and added it to "allowedCommonJsDependencies" but still the same warning. Any idea?Herve
@Twenty please have a look at this answer: https://mcmap.net/q/136878/-angular-10-upgrade-fix-commonjs-or-amd-dependencies-can-cause-optimization-bailoutsTerence
Altough advice is correct, it assumes it is I that uses CommonJS or AMD, while the most common scenario is that I am using 3rd party lib that dependes on those.Hussite
For lodash, use lodash-es instead: npmjs.com/package/lodash-esPossessory
A
33

For the RXJS library you can do the following changes.

For imports, such as 'rxjs/internal/<anything>' and 'rxjs/index', replace it with just 'rxjs'.

For imports such as 'rxjs/internal/operators', replace it with 'rxjs/operators'.

Or replace just rxjs.

Aesir answered 13/11, 2020 at 13:49 Comment(4)
Replacing with just 'rxjs' did the trick for me, thanksSuperimpose
Replacing with just 'rxjs' did the trick for me too, thanksChalcopyrite
Does this fix the issue or just hide the warning?Beeck
@Aesir I noted that it does. Shrank the size of my build a fair amount!Beeck
C
10

Just change the import:

from:

import { BehaviorSubject } from 'rxjs/internal/BehaviorSubject';

To:

import { BehaviorSubject } from 'rxjs';

Consensus answered 14/3, 2021 at 14:1 Comment(4)
Is this just suppressing the warning, or does this also help with the optimization?Gravel
@Gravel this is how we import BehaviorSubject in RxJS v6+ learnrxjs.io/learn-rxjs/subjects/behaviorsubjectConsensus
Yes I understand that, my question is wether the new way helps only with suppressing the compiler warning, or is v6+ indeed a new ES6 modulization for rxjs (while the older versions were CommonJS).Gravel
Hi @Gravel sorry for late replay. The RxJS 6 brings improvements in modularity, a boost in performance and easier to debug call stacks. The RxJS team has made a solid effort on making this release as backward compatible as possible. auth0.com/blog/whats-new-in-rxjs-6Consensus
M
6

Another case of this is the problem being warned about during the build with the use of BehaviorSubject from rxjs when using the following style of imports:

import { BehaviorSubject } from 'rxjs/BehaviorSubject';

It results in the following error:

Warning: my.service.ts depends on 'rxjs/BehaviorSubject'. CommonJS or AMD dependencies can cause optimization bailouts.

By importing from the root module instead, the warning is no longer present during the build:

import { BehaviorSubject } from 'rxjs';

Mooch answered 13/1, 2021 at 17:46 Comment(0)
F
5

to fix this issue on the terminal in angular.json put this line in :

{
  "architect": {
    "build": {
      "builder": "@angular-devkit/build-angular:browser",
      "options": {
        "allowedCommonJsDependencies": [
          "rxjs"
        ]
      }
    }
  }
}
Foretaste answered 10/11, 2022 at 6:1 Comment(1)
Is this a fix? This just hides the warning, correct? The problem is still there.Beeck
P
2

In my case (after update to TypeScript version 3.9.7) flatMap is deprecated (from rxjs/operators). This is alias for mergeMap, so just I replaced:

import { flatMap } from 'rxjs/internal/operators';

to

import { mergeMap } from 'rxjs/operators';

Poser answered 12/3, 2021 at 7:29 Comment(0)
O
2

I had a similar issue (app.module.ts depends on 'ngx-google-places-autocomplete'), but many answers did not help me.

So if you have x depends on y, just add y in the angular.json file in "allowedCommonJsDependencies".

Obstreperous answered 17/12, 2021 at 8:49 Comment(1)
This will only silence the warning, the dependencie will include the entire commonjs in your bundle leading to a bigger bundle size than necesarryCarpous
R
1

I was facing the same issue in Angular - 15.1.1 while build for the production

Add missing / suggested dependency in build -> allowedCommonJsDependencies

enter image description here

enter image description here

Roderick answered 2/8, 2023 at 10:45 Comment(0)
S
-2

I have a very big project with deprecated imports 'rxjs' and create this script for upgrading all deprecated imports:

python3.6 replace_imports.py PATH_TO_SRC_DIR

This script upgrades the import like "rxjs\/(internal|Observable|Subject|ReplaySubject|Subscription|BehaviorSubject)" to import { * } from rxjs

Also try to upgrade rxjs-compat.

Salts answered 15/4, 2021 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.