1

How do you expose jQuery on the window object using webpack?

I have this in my webpack config

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

But when loading an external node module which relies on this it fails. Checking in the console window.jQuery is undefined. JQuery is loaded and available as I'm using it elsewhere before I added the new third-party module.

Robin Elvin
  • 1,207
  • 12
  • 28

1 Answers1

0

I think youre misusing the ProvidePlugin. The provide plugin will automatically require the package when the varible you specify is is used within code. In order to put jquery in, on your entry point of the configuration do something like this.

window.jquery = require('jquery');
cgatian
  • 22,047
  • 9
  • 56
  • 76
  • How? The window object isn't available in the configuration and putting it in the entry point of my app is too late in the loading process. – Robin Elvin Jul 21 '16 at 08:38
  • Your entry point should actual be your entry point. If you have libraries being loaded in html prior to your bundle.js you're doing it wrong. – cgatian Jul 21 '16 at 12:10
  • This works for me: http://stackoverflow.com/questions/29080148/expose-jquery-to-real-window-object-with-webpack?rq=1 – Robin Elvin Jul 21 '16 at 13:43