rails sできない【webpackerがインストールされていない】

rails sを実行してサーバーを立ち上げようとしたら、以下のエラーが出ました。

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

環境

MacOS Mojave 10.14.4

Ruby 2.5.1

Rails 6.0.2.1

やったこと

Node.jsのインストール

rails webpacker:installを実行しろと書いてあるのでbundle exec 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 がインストールされてないと書かれているのでインストールします。 nodebrew install-binary latestコマンドを実行。

-bash: nodebrew: command not found

あれ、エラー。

そうだnodebrewをインストールしていなかった。。。

ということでnodebrewをインストールします。(※homebrewをインストールしていない人は先にこちらをインストールしてください)

brew install nodebrew

インストールされたっぽいのでバージョンを確認します。

nodebrew -vを実行。

nodebrew 1.0.1

インストールされていますね。

改めてnodebrew install-binary latestコマンドを実行します。

Fetching: https://nodejs.org/dist/v13.5.0/node-v13.5.0-darwin-x64.tar.gz
Warning: Failed to create the file 
Warning: /Users/ユーザー名/.nodebrew/src/v13.5.0/node-v13.5.0-darwin-x64.
Warning: tar.gz: No such file or directory
                                                                           0.0%
curl: (23) Failed writing body (0 != 1019)
download failed: https://nodejs.org/dist/v13.5.0/node-v13.5.0-darwin-x64.tar.gz

うん? どうやらsrcフォルダがないみたいなので作成します。

mkdir -p ~/.nodebrew/srcを実行して再度nodebrew install-binary latestを実行します。

Fetching: https://nodejs.org/dist/v13.5.0/node-v13.5.0-darwin-x64.tar.gz
######################################################################## 100.0%
Installed successfully

やっと成功しました。

次に今あるバージョンを確認するためnodebrew lsを実行します。

v13.5.0

current: none

v13.5.0はあるけど、今選択しているバージョンはないよと言っています。

なのでバージョンを選択するためnodebrew use v13.5.0を実行します。

nodeのバージョンを確認するためnode -vを実行。

-bash: node: command not found

どうやらパスを通さないとダメみたいです。

echo 'export PATH=$HOME/.nodebrew/current/bin:$PATH' >> ~/.bash_profileを実行します。

今すぐ変更を反映させたいのでsource ~/.bash_profileを実行します。

改めてnode -vを実行してバージョン確認。

v13.5.0

これでNode.jsのインストールが完了です。

yarnのインストール

Node.jsのインストールが終わったのでbundle exec rails webpacker:installを実行します。

今度こそ成功するでしょ!

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

またエラーorz

今度はyarnをインストールしろと言っています。

brew install yarnを実行してインストールします。

無事インストールできたみたいなのでバージョンを確認します。

yarn -vを実行して1.21.1

うん、できてますね。

これで再々度bundle exec rails webpacker:installを実行

なんか長い文字が流れてきて最後にWebpacker successfully installed 🎉 🍰が出てきました!

やっとwebpackerをインストールできました!

これでrails sを実行します。

Image from Gyazo

見慣れた画面が表示されました!

てかrails6でやってたのね。。。

まとめ

rails sを実行してPlease run rails webpacker:install Error: No such file or directoryと出たらrails webpacker:installを実行する

rails webpacker:instalまたはbundle exec rails webpacker:instalを実行してNode.js not installed.と出たらNode.jsをインストールする

rails webpacker:instalまたはbundle exec rails webpacker:instalを実行してYarn not installed.と出たらyarnをインストールする

余談

rails6はwebpackerのgemがデフォルトでインストールされていて、利用するのにnode.jsとyarnが必要みたいです。 (node.jsとyarnをいまいち理解していない)

参考

Node.js関連

MacにNode.jsをインストールする方法 | カフーブログ

MacにNode.jsをインストール - Qiita

yarn関連

Rails6 開発時につまづきそうな webpacker, yarn 関係のエラーと解決方法 - Qiita

Rails6 Webpackerでエラーが出た - Qiita