8

Is there any specific way to expose jQuery with Webpack 5?

It used to work on Webpack 4 OK, with the config bellow, but it shows the Uncaught Reference Error: jQuery is not defined error now with 5.

module: {
    rules: [
      {
        test: require.resolve('jquery'),
        loader: 'expose-loader',
        options: {
          exposes: ['$', 'jQuery'],
        },
      },
idm
  • 189
  • 1
  • 11

1 Answers1

1

Confirming @Sam Chen 's comment above, Webpack 5 (as of 5.75) does not require expose-loader to make $ available as an import. It was simplest to uninstall the loader, remove it from webpack.config.js, and add

import $ from 'jquery';

to all files that used it. This can get messy in the case of TypeScript projects with @types/jquery, as pages where the import is missing may not warn about it.

MBer
  • 2,218
  • 20
  • 34