'How to avoid duplication of module "bn.js" in webpack production build?

I used webpack 4 for my app. Somehow, bn.js package takes up a lot in production build.

Image 1

The image shows that it takes up 594.22 KB of data. Is there any way to have a 1 file of bn.js for all packages that depends on bn.js?



Solution 1:[1]

This likely happens because your dependencies all require different versions of bn.js. You could try to define one specific version in the resolutions list in your package.json:

{
  // ...
  "resolutions": {
    "bn.js": "^5.1.1"
  }
}

Keep in mind that newer versions might contain breaking changes which some of the dependencies depended on. You could evaluate your package-lock.json or yarn.lock to see what versions of bn.js are required throughout the dependencies and compare that with the changelog

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1