I want to expose the jQuery object to the global window object that is accessible inside the developer console in the browser. Now in my webpack config I have following lines:

plugins: [
                new webpack.ProvidePlugin({
                    $: 'jquery',
                    jQuery: 'jquery'
                })
            ]

These lines add the jQuery definitions to each file in my webpack modules. But when I build the project and try to access jQuery in the developer console like this:

window.$;
window.jQuery;

it says that these properties are undefined...

Is there a way to fix this?

推荐答案

You need to use the expose-loader.

npm install expose-loader --save-dev

You can either do this when you require it:

require("expose?$!jquery");

or you can do this in your config:

loaders: [
    { test: require.resolve('jquery'), loader: 'expose?jQuery!expose?$' }
]

UPDATE: As of webpack 2, you need to use expose-loader instead of expose:

module: {
    rules: [{
        test: require.resolve('jquery'),
        use: [{
            loader: 'expose-loader',
            options: '$'
        }]
    }]
}

Jquery相关问答推荐

如何用 jQuery / AJAX 替换表格的行

如何在 JavaScript 或 jQuery 中过滤 JSON 数据?

提醒未保存的表单更改

何时执行 $(document).ready 回调?

提交前添加 POST 参数

这些 jQuery 就绪函数之间有什么区别?

错误找不到 jquery-2.0.2.min.map

如何计算具有相同类的元素数量?

从 iframe 访问父窗口的元素

jQuery中的Grep与过滤器?

如何使用jQuery在弹出窗口中预览输入类型=文件中的选定图像?

jQuery 追加淡入

jquery,id内类的 Select 器

使用 jQuery 获取鼠标单击图像的 X/Y 坐标

更改 div 的内容 - jQuery

我应该在addClass之前使用hasClass吗?

自调用函数前的分号?

JS - 从base64代码中获取图片的宽高

IE 中带有 jQ​​uery ajax 调用的无传输错误

使用 JQuery 判断组中是否没有选中单选按钮