Search code examples
javascriptruby-on-railswebpackerruby-on-rails-6

How to add jquery third party plugin in rails 6 webpacker


I know its simple but with update of rails 6. there is new syntax in rails 6 for manage javascript assets which is maintained by webpacker.

//application.js
require("@rails/ujs") //.start()
require("turbolinks").start()
require("@rails/activestorage").start()
require('jquery').start()
require('jquery_ujs').start()
require('bootstrap-daterangepicker').start()
require("custom/custom").start()
require("bootstrap").start()
require("channels")

i am able to add custom/custom but bootstrap and jquery is not working i have install jquery and bootstrap via npm


Solution

  • run below command to add jQuery.

    $ yarn add jquery
    

    Add below code in config/webpack/environment.js

    const webpack = require('webpack')
    environment.plugins.prepend('Provide',
      new webpack.ProvidePlugin({
        $: 'jquery/src/jquery',
        jQuery: 'jquery/src/jquery'
      })
    )
    

    Require jquery in application.js file.

    require('jquery')
    

    No more need to add jquery-rails gem!