javascriptjquerywebpack

How to configure Webpack 5 bundle to use global jQuery?


I have a web page I'm loading jQuery on, with a script tag that points to a CDN. I'm loading jQuery into the global scope, and have small bits of JS throughout the site which use it - this is all working fine, and I'd like to continue loading jQuery this way.

Separate from that, I'm using using Webpack 5 to bundle several script files together. These scripts also use jQuery in them.

I'm just now learning that Webpack scopes its scripts. How do I get the Webpack-bundled scripts to be able to see and use my global jQuery?


Solution

  • You can try adding the following to your config:

    externals: ['jquery']
    
    

    See here about the webpack externals option.