LoginSignup
2
4

More than 3 years have passed since last update.

Angular 10でCommonJS形式のモジュールを読み込んだ際に起きる警告の対処法

Posted at

概要

Angular 10からCommonJS形式のモジュールを読み込むと警告が表示されるようになりました。
lodashのv4.17.19などを読み込んでみると再現できるかと思います。

解決策その1

理想的な解決方法はESM形式に切り替えることです。
先ほど例に挙げたlodashならlodash-esというESM形式のライブラリがリリースされてます。

解決策その2

とはいえすべてのライブラリがESM形式もリリースされているわけではありません。
その場合はangular.jsonでCommonJS形式でも許可するライブラリを指定することで警告を抑えることができます。
ただしあくまで警告を抑えるだけなのでビルド時の最適化などがされるわけではないようです。

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

参考リンク

https://blog.angular.io/version-10-of-angular-now-available-78960babd41
https://angular.io/guide/build#configuring-commonjs-dependencies

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4