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.
CommonJS is a specification which helps to load modules from one file to other file. Two JavaScript developers will follow CommonJS specifications in order make their module communicate with each other. In order to make it work practically, one file gets exposed while the other file will be using that exposed file.
Getting Started. From a structure perspective, a CommonJS module is a reusable piece of JavaScript that exports specific objects made available to any dependent code. Unlike AMD, there are typically no function wrappers around such modules (so we won't see define here, for example).
The npm package lodash
itself is not an ECMAScript module and therefore produces the warning. There are multiple ways to fix this:
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';
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:
"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 passlodash
, the sub-paths (e.g.lodash/keys
) will also be allowed.
Docs reference: https://angular.io/guide/build#configuring-commonjs-dependencies
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With