How to Use Webpacker with in Rails 6


Webpacker is a gem which wraps webpack. In rails 6 webpacker is the default JavaScript compiler instead of sprockets. To use webpacker you need to install the yarn package manager and you must have Node.js installed version 10.13.0 and up.

Updating our Gemfile

Update the gem in the gem file

After mentioning the webpacker gem with the latest version, we need to run the bundle install and it will update the gem. Now we can use the latest webpacker.

Install the WebPacker If not installed

If the webpacker does not exist in application then we need to install the Webpacker by the following command :  bin/rails webpacker:install it will install the webpacker in your application.

How to use Jquery with WebPacker

In rails application to use the jquery with webpacker it’s different. Here, we’ve shown how to require the Jquery.

Here we have required the Jquery and the app-globals.js is a file where the jquery is written and we need to change the environment.js file as well.Which is in the config/webpack/environment.js  in this directory.

Conclusion: In compiles and compress the Bable code to javascript. so First time it takes time to load in as it compiles the code. But after that, It work very smooth and fast.

About the author

Manish is the Ruby on Rails and React Expert Developer. He loves coding.
Interesting code area: ROR Web Application / ML Code / AI Code.

Leave a Reply

Your email address will not be published. Required fields are marked *