为什么当模块不需要时 webpack 会寻找 jQuery? [英] Why is webpack looking for jQuery when the module doesn't require it?

查看:42
本文介绍了为什么当模块不需要时 webpack 会寻找 jQuery?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在这个问题的开头说我很难理解这个问题到底发生了什么,如果它不符合 Stackoverflow 的问答标准,我深表歉意.

我正在尝试 import 一个 NPM 模块,然后由 Webpack 在我们的项目中编译该模块.

在模块的 package.json 或类似文件中,没有其他地方将 jQuery 描述为依赖项.我不知道发生了什么.如果有任何用处,这里是似乎在编译包中抛出错误的代码:

else if (typeofdefine === 'function' &&define.amd) {定义([],函数(){'使用严格';返回窗口.Swiper;});}//# sourceMappingURL=maps/swiper.js.map/* WEBPACK VAR 注入 */}.call(exports, __webpack_require__(!(function webpackMissingModule() { var e = new Error("Cannot find module \"jquery\""); e.code = 'MODULE_NOT_FOUND'; throw e; }()))))

解决方案

我认为 Swiper 本身需要 jQuery.因为您在 AMD/webpack 环境中,Swiper 正在尝试导入/需要"它,而 webpack 找不到它.

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; }())))) 

解决方案

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.

这篇关于为什么当模块不需要时 webpack 会寻找 jQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆