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

Viewed 210

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?

1 Answers

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

Related