26
19

More than 3 years have passed since last update.

Rails6サーバー起動時webpackerエラー(Node.jsアップデート, yarnインストール)

Posted at

内容

  • VagrantでRails6の環境構築をして、サーバー起動したときにwebpackerをインストールしろというエラーが発生したのでその対応
  • Rails6からwebpackerが標準となり、yarnのインストールが必要になったためエラーが発生した模様
  • 結論: webpackerをインストールしようとするとNodejsが古いと指摘を受けアップデート、yarnも必要となりインストール、その後webpackerインストールしたら解決できた

環境

  • macOS Mojave 10.14.6
  • VirtualBox 6.0.10
  • Vagrant 2.2.5
  • centos/7 (virtualbox, 1905.1)
  • rbenv 1.1.2-4-g577f046
  • ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
  • Rails 6.0.0
  • psql (PostgreSQL) 12.0
  • nodejs v12.12.0
  • Bundler version 2.0.2

エラー内容

# エラーを発生させたコマンド
$ bin/rails s -b 0.0.0.0 -p 2000



# エラー内容
=> Booting Puma
=> Rails 6.0.0 application starting in development 
=> Run `rails server --help` for more startup options
RAILS_ENV=development environment is not defined in config/webpacker.yml, falling back to production environment
Exiting
Traceback (most recent call last):
...
略
...
/home/vagrant/share/vendor/bundle/ruby/2.6.0/gems/webpacker-4.0.7/lib/webpacker/configuration.rb:91:in `rescue in load': Webpacker configuration file not found /share/sample_app/config/webpacker.yml. Please run rails webpacker:install Error: No such file or directory @ rb_sysopen - /share/sample_app/config/webpacker.yml (RuntimeError)

やったこと

webpackerインストール

エラー文が長々とありましたが、最後の方にPlease run rails webpacker:installとあるので素直に従う

$ rails webpacker:install
Webpacker requires Node.js >= 6.14.4 and you are using 4.9.1
Please upgrade Node.js https://nodejs.org/en/download/

どうやらNode.jsが古いとのこと

Node.jsアップデート

Node.jsが古いと指摘が会ったのでアップデート
Node.jsのバージョン管理ツールnvmを使用してインストールする

# 現在のバージョン確認
$ node -v
v4.9.1

# Node.jsのバージョン管理ツールnvmをclone
$ git clone git://github.com/creationix/nvm.git ~/.nvm
$ echo . ~/.nvm/nvm.sh >> ~/.bashrc
$ . ~/.bashrc

# nvmバージョン確認
$ nvm --version
0.35.0

# インストールできるNode.jsの確認
$ nvm ls-remote

# 最新の安定版をインストール
$ nvm install stable

# バージョン確認
$ node -v
v12.12.0

アップデート完了したので再度webpackerインストール

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

今度はyarnが必要とのこと

yarnインストール

yarnインストールのエラーが発生したので従う

# yarn最新版を取ってくる
$ curl --silent --location https://dl.yarnpkg.com/rpm/yarn.repo | sudo tee /etc/yum.repos.d/yarn.repo
[yarn]
name=Yarn Repository
baseurl=https://dl.yarnpkg.com/rpm/
enabled=1
gpgcheck=1
gpgkey=https://dl.yarnpkg.com/rpm/pubkey.gpg


# yarnをインストール
$ sudo yum -y install yarn

# バージョン確認
$ yarn -v
1.19.1

# package.jsonリスト化されている全ての依存関係をインストール
# railsのプロジェクトの中で次のコマンド実行
$ yarn install

yarnインストールが完了したので再々度webpackerインストール

$ rails webpacker:install
Webpacker successfully installed 🎉 🍰

インストール完了したら再度サーバー起動

$ bin/rails s -b 0.0.0.0 -p 2000

スクリーンショット 2019-10-19 17.14.08.png

(参考)
https://tako1192.hatenablog.com/entry/2016/05/07/054522

26
19
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
26
19