Rails 6 webpack throwing 'Uncaught ReferenceError: $ is not defined'

二次信任 提交于 2020-12-05 07:24:32

问题


Hi i recently started working on Rails 6 with ruby 2.6.5. as the rails 6 has introduced the webpacks so i am trying to load my js files with the webpack. Although i have already required the jquery in the appliation.js still i am getting the below error:

'Uncaught ReferenceError: $ is not defined'

application.js

require("@rails/ujs").start()
require("turbolinks").start()
require("@rails/activestorage").start()
require("channels")
require('packs/jquery')

My 'jquery.js' file is inside the packs directory. Please let me know if i am missing something. Thanks in advance :)


回答1:


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

const webpack = require('webpack')
environment.plugins.prepend('Provide', new webpack.ProvidePlugin({
  $: 'jquery/src/jquery',
  jQuery: 'jquery/src/jquery',
  jquery: 'jquery',
  'window.jQuery': 'jquery',
  Popper: ['popper.js', 'default']
}))

module.exports = environment

In config/webpack/environment.js and delete require('packs/jquery') in application.js



来源:https://stackoverflow.com/questions/58677275/rails-6-webpack-throwing-uncaught-referenceerror-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!