Why is webpack looking for jQuery when the module

2019-09-20 13:22发布

问题:

I'd like to preface this question by saying it is hard for me to understand what exactly is going on in this issue and I apologise if it's not up to Stackoverflow's Q&A standards.

I'm trying to import an NPM module that's then compiled by Webpack on our project. This is the module in question, and this is the file that's giving us problems.

At the end of that file, there is the export code:

if (typeof(module) !== 'undefined')
{
    module.exports = window.Swiper;
}
else if (typeof define === 'function' && define.amd) {
    define([], function () {
        'use strict';
        return window.Swiper;
    });
}

And that's it. Throughout the file there are some references to jQuery but nothing that suggests webpack should include it as a module. Do a search for "jQuery" and see for yourself.

So why is it then when we import the plugin like so:

import Swiper from 'swiper'

We get the following error?

No where else in the module's package.json or similar does it describe jQuery as a dependency. I have no idea what's happening. If it's any use, here is the code that seems to throw the error in the compiled bundle:

else if (typeof define === 'function' && define.amd) {
        define([], function () {
            'use strict';
            return window.Swiper;
        });
    }
    //# sourceMappingURL=maps/swiper.js.map

    /* WEBPACK VAR INJECTION */}.call(exports, __webpack_require__(!(function webpackMissingModule() { var e = new Error("Cannot find module \"jquery\""); e.code = 'MODULE_NOT_FOUND'; throw e; }())))) 

回答1:

I think Swiper itself requires jQuery. Because you are in an AMD/webpack environment, Swiper is trying to "import/require" it and webpack cannot find it.