Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8934

Rails6でアプリ起動時にWebpacker configuration file not foundエラーが発生した時の対処方法

$
0
0

執筆背景

scaffoldを使って簡単なアプリを作ろうと以下のコマンドを打ち込むと、Webpackerがインストールされていないというエラーが発生したため、記述しておこうと思う。

$ rails new app_name
$ cd app
$ rails g scaffold post content:text
$ rails s

(中略)
Webpacker configuration file not found /Users/ユーザ名/app_name/config/webpacker.yml.
Please run rails webpacker:install 
Error: No such file or directory 
@ rb_sysopen - /Users/ユーザ名/app_name/config/webpacker.yml 
(RuntimeError)

ちなみに、このときのrubyのバージョンは2.7.0、railsのバージョンは、6.0.2.2です。

結論

このエラーは、webpackerがインストールされていない場合に起こるエラーみたいです(rails6からwebpackerが標準になったため)。

webpackerのインストールには、Node.jsとYarnのインストールが必要で、これらをインストールした上でwebpackerをインストールすると解決します。

作業内容

Gemfileを確認し、gem 'webpacker'の記述を確認。

Gemfile
gem'webpacker','~> 4.0'

bundle installしたのち、webpackerをインストールしようと試みるもNode.jsがインストールされていないと怒られる。

$ bundle install
$ rails webpacker:install
sh: node: command not found
sh: nodejs: command not found
Node.js not installed. Please download and install Node.js https://nodejs.org/en/download/

Node.jsをインストールしたのち、再度webpackerのインストールを試みると、Yarnをインストールしろと怒られる。

$ brew install node
(中略)
==> node
Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

$ rails webpacker:install
Yarn not installed. 
Please download and install 
Yarn from https://yarnpkg.com/lang/en/docs/install/

Yarnをインストールしたのち、再度webpackerのインストールを試みたところインストールすることができた。

$ sudo npm install -g yarn
added 1 package in 3.509s

$ yarn -v
1.22.4
$ yarn install
success Saved lockfile.
✨  Done in 3.49s.

$ rails webpacker:install
✨  Done in 6.34s.
Webpacker successfully installed 🎉 🍰

アプリを再起動。

$ rails s
Use Ctrl-C to stop
Started GET "/posts/index" for ::1 at 2020-03-28 18:06:09 +0900

うまくいった!
スクリーンショット 2020-03-28 18.07.56.png


Viewing all articles
Browse latest Browse all 8934

Trending Articles