$ is not defined when installing jQuery in Rails via Webpack

后端 未结 5 1425
猫巷女王i
猫巷女王i 2021-02-18 19:04

I\'m trying to install jQuery in Rails 6.0.0.rc1 via Webpack and I\'m not sure what I\'m missing but I\'m getting the error $ is not defined in the browser console

5条回答
  •  说谎
    说谎 (楼主)
    2021-02-18 19:15

    I tried a lot of things and some worked and some didn't, and some didn't work in my Docker container. In the end I settled on putting this in app/javascript/packs/application.js:

    global.$ = require('jquery')
    

    You need to do yarn add jquery of course.

    I'm sure there are better ways but this is the only thing that worked for me so I'm putting it up here in case it helps anyone else.

提交回复
热议问题