简体   繁体   中英

Webpack multiple requires resolving to same file but being imported twice

Hello I just had this problem with webpack. If I do require('../something') from one file, and then I do require('../../something') in another file they both end up resolving to the same file. However if you look in the output bundle there are two different webpack functions both with the same content. I'm pretty sure I can use an alias to fix this and then just do require('something') in both files. But is this the right way to do it or am I missing something?

Btw I need this because it's causing several problems with angularjs undefining my controllers.

You can just use the DedupePlugin. It looks for if the module has already been included in your build and if so, doesn't include it again. It's easy to set up and you don't need to require or install anything extra.

var webpack = require("webpack");

module.exports = {
    // more of your config
    // ...
    plugins: [
        new webpack.optimize.DedupePlugin()
    ]
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM