简体   繁体   English

webpack 无法解析“jquery”

[英]webpack Can't resolve 'jquery'

Hi I'm a new to Webpack , currently I'm adding this tool to my project, during bundling (webpack ...) jquery-dependent library I get an error like that:嗨,我是 Webpack 的新手,目前我正在将此工具添加到我的项目中,在捆绑 (webpack ...) 依赖于 jquery 的库期间,我收到如下错误:

Error: Can't resolve 'jquery' in 'C:\\some_folder'错误:无法解析“C:\\some_folder”中的“jquery”

I browsed through similar posts on internet with no positive effect.我浏览了互联网上的类似帖子,但没有任何积极影响。 People recommend using different approaches like : - resolve.alias - plugins ProvidePlugin人们建议使用不同的方法,例如:-resolve.alias-pluginsProvidePlugin

I use webpack 3.3.0, in my project jQuery is loaded in normal way via script tag before vendor bundle scripts.我使用 webpack 3.3.0,在我的项目中, jQuery在供应商捆绑脚本之前通过脚本标记以正常方式加载。 Most of vendor librairies including jQuery live not in node_modules folder.大多数供应商库(包括 jQuery)不在 node_modules 文件夹中。

webpack.config.js: webpack.config.js:

const webpack = require('webpack');
const { resolve } = require('path');

module.exports = env => {
    return {
        context: resolve('src'),
        entry: {
            comp: './start.comp.js',
            culture: './start.culture.js',
            strategy: './start.strategy.js',
            vendors: './start.vendors.js'
        },
        output: {
            path: resolve('dist'),
            publicPath: '/dist/',
            filename: 'bundle.[name].js'
        },
        devtool: env.dev ? 'eval' : 'source-map'
    };
};

the last entry jquery.placeholder.min.js is a problem最后一个条目 jquery.placeholder.min.js 有问题

require('./../assets/vendor/typeahead.js');
require('./../assets/vendor/hogan-2.0.0.js');
require('./../assets/vendor/swfobject.js');
require('expose-loader?_!./../assets/vendor/underscore-min.js');
require('expose-loader?FastClick!./../assets/vendor/fastclick.js');
require('expose-loader?AOS!./../assets/vendor/aos.js');
require('./../assets/vendor/jquery.placeholder.min.js');

Problem问题

Since jquery.placholder.min.js is using UMD as its loading strategy, it's recognizing that it is required via a require - and tries to require jQuery in the same way:由于jquery.placholder.min.js使用 UMD 作为其加载策略,它通过require认识到它是必需的 - 并尝试以相同的方式要求 jQuery:

"object"==typeof module&&module.exports?require("jquery"):jQuery

Webpack sees require("jquery") and tries to bundle the jQuery library (which does not exist in the node_modules). Webpack 看到require("jquery")并尝试捆绑 jQuery 库(在 node_modules 中不存在)。

Solution解决方案

The solution is to add jQuery as an external in your webpack.config.js :解决方案是在webpack.config.js添加 jQuery 作为外部

{
  ...
  externals: {
    // require("jquery") is external and available
    //  on the global var jQuery
    "jquery": "jQuery"
  }
}

When a module is marked as an external , Webpack knows not to bundle it, but instead use the global variable.当一个模块被标记为external 时,Webpack 知道不捆绑它,而是使用全局变量。

I was working in react i have found that when i import the react-bootstrap components sometimes the components come like {Buttons} from bootstarp ;我在React工作时发现,当我导入react-bootstrap组件时,有时这些组件就像来自bootstarp 的{Buttons} ; then i got this error.然后我得到了这个错误。 But i solved it by calling the path from bootstrap to react-bootstrap ;但是我通过调用从bootstrapreact-bootstrap的路径解决了这个问题;

Check your auto import call and try changing it.检查您的自动导入调用并尝试更改它。 this worked for me.这对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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