To include Webpacker in a new project, add --webpack to the rails new command. To add Webpacker to an existing project, add the webpacker gem to the project's Gemfile , run bundle install , and then run bin/rails webpacker:install . The installation also calls the yarn package manager, creates a package.
Method: Webpacker::Helper#javascript_pack_tagCreates a script tag that references the named pack file, as compiled by webpack per the entries list in package/environments/base. js. By default, this list is auto-generated to match everything in app/javascript/packs/*. js.
Webpacker is a gem which allows easy integration of JavaScript pre-processor and bundler with Rails. It provides various helpers and configuration options to use webpack easily with Rails.
Since Rails 6, Webpacker is the default JavaScript compiler. So you'll also have to set it up before starting your Rails server.
yarn
using your OS package manager, or take a look at https://yarnpkg.com/en/docs/install
rails webpacker:install
yarn install --check-files
rails s
Enjoy!
To add onto what has been said, make sure to check the error message for instructions on how to go about resolving the issue. In this case the error says:
..."Please run rails webpacker:install"...
Once you run rails webpacker:install
you'll see the error that you don't have yarn installed, in which case you can go to the Installation Instructions to figure out how to install yarn for your operating system. For unix (I'm on a unix Amazon Cloud9 environment) I had to run:
curl -o- -L https://yarnpkg.com/install.sh | bash
Then I was able to successfully run rails webpacker:install
and then once I ran rails server
I saw the error:
========================================
Your Yarn packages are out of date!
Please run `yarn install --check-files` to update.
========================================
After that was installed, rails s
worked just fine.
I hope that gives more insight into how to go about "debugging" the issue!
After installing Rails, and setting up new application, once i tried to run rails server
, i faced this error.
I checked config/
and webpack configuration file was missing.
Following way i could fix it and made it working 100%.
Go to root directory of your rails application and follow instruction below.
bundle exec rails webpacker:install
command.
If your system doesn't have nodejs, you need to install nodejs
from https://nodejs.org/en/download/
Once Nodejs is installed, it might throw another error, yarn not installed
in that case,
Run command
npm install --global yarn
and after sorting all above, i was able to run rails server
and thus working.
Above command was performed in MacOS Mojave, Please check corresponding OS command to install both node/yarn before proceeding.
To see complete installation and running instruction you can check: https://medium.com/intelligent-computing/up-and-running-your-first-ruby-on-rails-app-with-troubleshooting-83e694d87605
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With