広告 Ruby on Rails

rails webpacker:install Errorの対処法

【Rails】Webpackerに関するエラーが出た時の対処法

Webpackerのエラー内容

「rails new test_app」でrailsアプリケーションを作成後、サーバーを起動しようとするとWebpackerに関するエラーが発生。

PS C:\ruby\test_app> rails s       
=> Booting Puma
=> Rails 6.0.3.2 application starting in development
=> Run `rails server --help` for more startup options
Exiting
Traceback (most recent call last):
        49: from bin/rails:4:in `<main>'
        48: from bin/rails:4:in `require'
        47: from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-6.0.3.2/lib/rails/commands.rb:18:in `<top (required)>'
        46: from C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/railties-6.0.3.2/lib/rails/command.rb:46:in `invoke'


・・・(省略)・・・

C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/webpacker-4.2.2/lib/webpacker/configuration.rb:95:in `rescue in load': Webpacker configuration file not found C:/ruby/test_app/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - C:/ruby/test_app/config/webpacker.yml (RuntimeError)

スポンサーリンク

Webpackerエラーの解決方法

エラーメッセージを見ると「rails webpacker:install」を実行してWebpackerをインストールしてくださいと書かれているので、Webpackerをインストールしてみます。

PS C:\ruby\test_app> rails webpacker:install
Yarn not installed. Please download and install Yarn from https://yarnpkg.com/lang/en/docs/install/

ただ上記のようにyarnがインストールされていないと、Webpackerをインストールできないので、まずはyarnをインストールします。

Yarnのインストール

Macの場合は次のコマンドでyarnをインストールすることができます。

brew install yarn

Windowsの場合は、次の記事をご覧ください。

Webpackerのインストール

yarnのインストールが完了したら「rails webpacker:install」コマンドによりWebpackerをインストールします。

PS C:\ruby\test_app> rails webpacker:install
      create  config/webpacker.yml
Copying webpack core config
      create  config/webpack
      create  config/webpack/development.js
      create  config/webpack/environment.js
      create  config/webpack/production.js
      create  config/webpack/test.js

・・・(省略)・・・

Done in 8.65s.
Webpacker successfully installed

「Webpacker successfully installed」と表示されればインストールは完了です。

サーバーを起動

再び「rails s」コマンドでサーバーを起動します。

PS C:\ruby\test_app> rails s
=> Booting Puma
=> Rails 6.0.3.2 application starting in development
=> Run `rails server --help` for more startup options
*** SIGUSR2 not implemented, signal based restart unavailable!
*** SIGUSR1 not implemented, signal based restart unavailable!
*** SIGHUP not implemented, signal based logs reopening unavailable!
Puma starting in single mode...
* Version 4.3.5 (ruby 2.7.1-p83), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
* Listening on tcp://[::1]:3000
* Listening on tcp://127.0.0.1:3000
Use Ctrl-C to stop

エラーメッセージは出なくなりました。

最後に「http://localhost:3000/」にアクセスし Railsの画面が表示されればサーバーの起動は成功です。

railsのウェルカムページ

 

helpful