How to let Webpack require a root node_module instead of an child package?

346 Views Asked by At

I have installed backbone and backbone.babysitter trough npm. When I use backbone in my scripts like this: import Backbone from "backbone"; It loads the installed backbone version 1.2.1. This works fine until I want to use backbone.babysitter. When backbone.babysitter loads it needs to add properties to backbone itself. But the package of backbone.babysitter imports its own backbone as dependency in his own node_modules folder, this backbone is on 1.2.0. So it attaches his methods to a different backbone i am working with.

How can I force Webpack to require the backbone from the root node_modules folder for backbone.babysitter?

2

There are 2 best solutions below

0
On BEST ANSWER

Found a workaround here

module.exports = {
  resolve: {
    alias: {
      'backbone': require.resolve('backbone')
    }
  }
}
0
On

You can use NormalModuleReplacementPlugin to rewire all require('backbone') towards your root backbone module.

plugins: [
  new webpack.NormalModuleReplacementPlugin(/^backbone$/, require.resolve('backbone')),
],