question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Duplicate module loaded multiple times

See original GitHub issue

Hi,

Let’s say I have a project with a dependency tree like (all dependencies being declared as “dependencies” in package.json)

  • Main application
    • angular
    • Sub-module 1
      • angular
    • Sub-module 2
      • angular

When bundling my app, I will end-up with angular being included 2 times in my webpage. I know I can use webpack.optimize.DedupePlugin() to prevent the file being included multiple times but it will still be called two times at runtime, resulting in a warning “tried to load angular more than once”.

I feel this is the same for every sub-dependencies shared by my app dependencies, every sub-module will have its own version of angular and even though they use the exact same version, it will be loaded multiple times.

Is this tied to the philosophy of not having a flat dependency tree or am I doing something wrong ? One solution I have found is to move all the dependencies of my sub-modules as peerDependencies and add them as dependencies to my “Main application” but I feel like this is a very bad solution.

Any insights on this ?

Thanks.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Reactions:47
  • Comments:24 (1 by maintainers)

github_iconTop GitHub Comments

44reactions
mxduboiscommented, Mar 5, 2016

You probably do want angular to be a peerDependency, since it is a framework within which your code runs. That’s how I’ve modeled it in an angular component library we use internally at my company.

You can then ensure that only one instance of angular is loaded from your main module by adding an alias for angular to your webpack config:

// webpack.config.js
import path from 'path';
// ...

export default {
  resolve: {
    alias: {
      'angular': path.resolve(path.join(__dirname, 'node_modules', 'angular'))
     },
     //...
  },
  // ...
}

For reference, another possible way to handle this is to add angular to externals and load it onto the page separately through a <script> tag or a separate bundle. This solution is useful if you’re building your submodules independently as UMD libraries.

36reactions
hungtran0203commented, Jul 13, 2016

Is there any solution for this problem? I’m having the same problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What Happens When a Module Is Imported Twice?
A JavaScript module is evaluated just once. When imported multiple times from the same path, the same module instance is returned.
Read more >
Duplicate modules of same version in webpack build
I understand that modules of different versions might need to be duplicated to avoid errors, but even if yarn has the sub modules...
Read more >
Finding and fixing duplicates in webpack with Inspectpack
(Note that npm and webpack have changed how they handle duplicated packages and code, so we're going to cover the mechanics of each...
Read more >
CommonJS modules | Node.js v19.3.0 Documentation
To have a module execute code multiple times, export a function, and call that function. Module caching caveats#. Modules are cached based on...
Read more >
NgModule FAQ - Angular
When two imported modules, loaded at the same time, list a provider with the same token, the second module's provider "wins". That's because...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found