Originally Published: 2020-09-24
$ is not defined,” particularly if you’re trying to use jQuery from your
I’ve come across a huge number of confusing and outdated answers for how to fix this, so I’m sharing what worked for me at the time of this writing (Rails 18.104.22.168, webpacker 5.2.1).
The best way seems to be to use the
expose-loader module to expose jQuery from your webpack config. For me this meant running
yarn add expose-loader to install the module, then updating my
config/webpack/environment.js to expose jQuery as
For me, the
environment.loader.append block is the only new addition here; if you haven’t already installed jQuery and aren’t already loading
jquery as a plugin, you may need to do that as well. Note that this should work with
expose-loader 1.0.0—other solutions I found used a syntax that was incompatible with this version, and would result in the error:
Expose Loader has been initialized using an options object that does not match the API schema. - options misses the property 'exposes'