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

1

133

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

WARNING in calendar.reducer.ts depends on lodash/keys. CommonJS or AMD dependencies can cause optimization bailouts.

I have added below line to my angular.json file but issue is not resolved

"allowedCommonJsDependencies": ["lodash"]

How can I fix above issue.

Intercellular answered 26/6, 2020 at 6:29 Comment(1)
This question is not a duplicate of the linked question. It has been asked and answered earlier. Additionally, the other question is about RxJS, this question is about lodash. They are similar, but not duplicate. Please re-open.Mackenie
M
224

The npm package lodash itself is not an ECMAScript module and therefore produces the warning. As a result, you may have a significantly increased bundle size, as the compiler is not able to perform tree-shaking. Here is how you can get rid of the warning:

Replace with ES modulized library (recommended)

Some libraries offer ES modulized builds. In case of lodash, you can replace it with lodash-es.

Run npm install --save lodash-es.

Now replace all imports from lodash with lodash-es. Also make sure to import the library with ES import statements:

import { keys } from 'lodash-es';

This will ensure that the compiler can optimize out unused symbols from the package.

Whitelist CommonJS dependency

If there is no ES modulized build available for your library, or if you for some reason don't care, you can allow specific CommonJS dependencies in the angular.json file (this may significantly increase the bundle size of your application):

"architect": {
  "build": {
    "builder": "@angular-devkit/build-angular:browser",
    "options": {
      "allowedCommonJsDependencies": ["lodash"]
    }
  }
}

Since Angular CLI Version 10.0.1 you can use globs in allowedCommonJsDependencies. This means that if you pass lodash, the sub-paths (e.g. lodash/keys) will also be allowed.

Docs reference: https://angular.io/guide/build#configuring-commonjs-dependencies

Mackenie answered 26/6, 2020 at 6:33 Comment(12)
Tried it but issue still existIntercellular
Imported like this - import keys from 'lodash/keys'Intercellular
Getting error like cannot find module 'lodash-es', do I need to npm install 'lodash-es' ?Intercellular
yes you need to install lodash-es. I'm updating my answer to make it more clearMackenie
Property allowedCommonJsDependencies is not allowed.Inness
@Cleancode did you put it to the right place? (architect - build - options) Updated my answer, now it should be more clear where to put it.Mackenie
Yes, I did and it was working before. Just cleaned the npm cache and let me try reinstalling the packagesInness
Here is a packaged version of lodash-es : npmjs.com/package/lodash-es. Now you can build it yourself with lodash CLI.Donovan
@Basheer Kharoti make sure you put it in "architect": { "build": { "builder": "@angular-devkit/build-angular:browser", "options": {}Chloro
@BasheerKharoti it should go away after npm installLanthanide
Whitelist CommonJS dependency for lodash and moment works for me. sample: "allowedCommonJsDependencies": ["lodash", "moment"],Freehold
I get this for 'moment.js'. What is his ES version which I can install in Angular project?Barogram

© 2022 - 2024 — McMap. All rights reserved.