177

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ora
  • 1,889
  • 2
  • 9
  • 9
  • Can you check if you once did not import from rxjs instead from rxjs/behaviorsubject. – Jonathan Stellwag Jun 26 '20 at 11:10
  • 8
    @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. – Ora Jun 26 '20 at 11:23
  • 3
    Does this answer your question? [Angular 10 Upgrade - Fix CommonJS or AMD dependencies can cause optimization bailouts](https://stackoverflow.com/questions/62589229/angular-10-upgrade-fix-commonjs-or-amd-dependencies-can-cause-optimization-bai) – JSON Derulo Jul 14 '20 at 13:28

11 Answers11

187

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 ... 
     ]
     ...
   }
   ...
},
Gunjan Khanwilkar
  • 2,227
  • 2
  • 11
  • 13
  • 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-dependencies` – Leonardo Rick Aug 15 '20 at 18:15
  • 3
    @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. – Gunjan Khanwilkar Aug 15 '20 at 19:42
  • 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 one – Leonardo Rick Aug 15 '20 at 21:19
  • @GunjanKhanwilkar any chance you improve your answer explaining clearly why packaging with CommonJS can result in larger slower applications? – freedev Sep 07 '20 at 09:31
  • 1
    @freedev I have updated the link in my answer! You could find a good explanation here - https://web.dev/commonjs-larger-bundles/ Cheers! – Gunjan Khanwilkar Sep 08 '20 at 13:30
  • 2
    For anyone who searches for the file where it should be added - `angular.json` in the root directory of your project. – Maxím G. Mar 21 '21 at 16:11
  • 1
    @MaximGeorgievskiy Good catch newbies might have difficulties to locate the file.. I have updated the answer for the same thanks! – Gunjan Khanwilkar Mar 22 '21 at 13:35
  • 1
    worked angular@12 – mstgnz Dec 20 '21 at 05:38
  • works like a charm for `sweetalert2` and `moment` on Angular 14 – Stefanos Chrs Jan 25 '23 at 08:59
113

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';
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Janardhan Burle
  • 1,417
  • 1
  • 4
  • 7
58

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Arun Kumar
  • 1,449
  • 1
  • 17
  • 33
31

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.

ymssa___
  • 993
  • 9
  • 16
9

Just change the import:

from:

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

To:

import { BehaviorSubject } from 'rxjs';

Adel Kedjour
  • 91
  • 1
  • 2
  • 3
    Is this just suppressing the warning, or does this also help with the optimization? – liakoyras Apr 14 '21 at 09:18
  • @liakoyras this is how we import `BehaviorSubject ` in RxJS v6+ https://www.learnrxjs.io/learn-rxjs/subjects/behaviorsubject – Adel Kedjour Apr 15 '21 at 10:50
  • 1
    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). – liakoyras Apr 15 '21 at 12:24
  • 2
    Hi @liakoyras 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. https://auth0.com/blog/whats-new-in-rxjs-6/ – Adel Kedjour Apr 19 '21 at 09:27
5

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';

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
atconway
  • 20,624
  • 30
  • 159
  • 229
3

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"
        ]
      }
    }
  }
}
Kishan Donga
  • 2,851
  • 2
  • 23
  • 35
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';

wajdar_pl
  • 21
  • 1
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".

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dacili
  • 258
  • 4
  • 8
  • This will only silence the warning, the dependencie will include the entire commonjs in your bundle leading to a bigger bundle size than necesarry – Benjamin Aronsson Dec 21 '21 at 19:20
0

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

Abdullah
  • 2,393
  • 1
  • 16
  • 29
-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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
mego4iter
  • 320
  • 1
  • 2
  • 9