5

I am using Rails 6.0.3.2, ruby 2.7.1, yarn 1.22.0.

I am trying to do normal UJS stuff, and this is the error I am getting:

VM125:1 Uncaught ReferenceError: $ is not defined
    at <anonymous>:1:1
    at processResponse (rails-ujs.js:283)
    at rails-ujs.js:196
    at XMLHttpRequest.xhr.onreadystatechange (rails-ujs.js:264)

This is my package.json:

{
  "name": "myapp",
  "private": true,
  "dependencies": {
    "@rails/actioncable": "^6.0.0",
    "@rails/actiontext": "^6.0.2-2",
    "@rails/activestorage": "^6.0.0",
    "@rails/ujs": "^6.0.0",
    "@rails/webpacker": "4.2.2",
    "bootstrap": "^4.4.1",
    "data-confirm-modal": "^1.6.2",
    "expose-loader": "^1.0.0",
    "flickity": "^2.2.1",
    "jquery": "3.4.1",
    "local-time": "^2.1.0",
    "popper.js": "^1.16.1",
    "stimulus": "^1.1.1",
    "trix": "^1.0.0",
    "turbolinks": "^5.2.0"
  },
  "version": "0.1.0",
  "devDependencies": {
    "webpack-dev-server": "^3.10.3"
  }
}

This is my environment.js:

const { environment } = require('@rails/webpacker')

const webpack = require('webpack')
environment.plugins.append('Provide', new webpack.ProvidePlugin({
  $: 'jquery',
  jQuery: 'jquery',
  Rails: '@rails/ujs'
}))

// environment.loaders.append('jquery', {
//     test: require.resolve('jquery'),
//     use: [{
//         loader: 'expose-loader',
//         options: '$',
//     }, {
//         loader: 'expose-loader',
//         options: 'jQuery',
//     }],
// });

module.exports = environment

As you can see from the commented out code above, I also tried using expose-loader per this answer but that gave me other errors.

In my application.js, I have the following:

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require("local-time").start()
require("jquery")

import '../src/jquery.min'
.
.
.
import "controllers"

require("trix")
require("@rails/actiontext")

How do I fix this?

2
  • 1
    jQuery has been imported twice. Remove import '../src/jquery.min' and require jQuery before ujs. Commented Jul 17, 2020 at 3:07
  • @RaviTejaGadi I just tried that and it didn't fix the issue. It does clean up my code, so I appreciate the tips, but it didn't fix the error. Commented Jul 17, 2020 at 3:29

2 Answers 2

8

Add below code in : app/javascript/packs/application.js

window.jQuery = $;
window.$ = $;

So jQuery keywords could be picked up.

Sign up to request clarification or add additional context in comments.

2 Comments

So this is the weirdest thing. I tried this before, but perhaps I had put it too high in the file, cuz I just tried it again and it works like a charm. Thanks!
This is such a random horrible issue, but this fixed it!
0

I had a lot of issues migrating from webpacker 5 to shakapacker. Adding

import "expose-loader?exposes=$,jQuery!jquery"

To the top of application.js fixed the jQuery issues

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.