Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$ is not defined when use webpack.ProvidePlugin

I'm trying to use jquery with webpack but even though I use Webpack Provider plugin like:

new webpack.ProvidePlugin({
    'window.jQuery': 'jquery',
    'window.$': 'jquery',
    $: 'jquery'
})

Seems like does not expose my jQuery to window global scope.

like image 961
Leonan Luppi Avatar asked Dec 14 '25 18:12

Leonan Luppi


1 Answers

As I understand it, the ProvidePlugin provides those variables to other WebPacked code but not external code or inline scripts.

The 'expose-loader' plugin alternately exposes global variables outside of the webpacked bundles. Add the following to your rules to define global jQuery and $ variables that point to your bundled jQuery.

rules:[
...
// Expose jquery globally for inline/legacy use
{
    test: require.resolve('jquery'),
    use: [
        { loader: 'expose-loader', options: 'jQuery' },
        { loader: 'expose-loader', options: '$' }
    ]
},
...
]
like image 178
James White Avatar answered Dec 17 '25 10:12

James White



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!