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
を実行します。
見慣れた画面が表示されました!
てか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をインストールする方法 | カフーブログ