I've been working on migrating a legacy Rails app to using webpacker in Rails 6.0.2, but keep coming across an Uncaught ReferenceError: jQuery is not defined
error. This same error appears even in a fresh Rails 6 app that I create.
I've followed plenty of different tutorials / StackOverflow answers, but nothing seems to be working. Here's what I currently have - can anyone please point me in the right direction?
I've run yarn add jquery
so this is in my package.json
file:
{
"name": "test_app",
"private": true,
"dependencies": {
"@rails/actioncable": "^6.0.0",
"@rails/activestorage": "^6.0.0",
"@rails/ujs": "^6.0.0",
"@rails/webpacker": "4.2.2",
"bootstrap": "^4.4.1",
"jquery": "^3.5.0",
"popper.js": "^1.16.1",
"turbolinks": "^5.2.0"
},
"version": "0.1.0",
"devDependencies": {
"webpack-dev-server": "^3.10.3"
}
}
In my application.js
file:
require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("jquery") // I've also tried without this line.
require("bootstrap");
And in my environment.js
file:
const { environment } = require('@rails/webpacker')
var webpack = require('webpack');
environment.plugins.append('Provide', new webpack.ProvidePlugin({
$: 'jquery/src/jquery', // I've also tried with just 'jquery' rather than referring specifically to the src folder.
jQuery: 'jquery/src/jquery' // Same comment as above.
}));
module.exports = environment
I'm pretty baffled as to why it's not working - I've tried the solutions suggested here, as well as a multitude of other resources. Any help would be greatly appreciated!