3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

webpackerを導入したRailsアプリケーションをHerokuにデプロイした時にyarnのバージョンでエラーが出る問題

Posted at

Railsにwebpackerが導入されてJavaScriptの資産を用いたフロントエンドの開発が行いやすくなりました。
実際にwebpackerを導入してローカルで動くアプリケーションが出来たので試しにHerokuにデプロイして動作確認しようとしてみたのですが、その際に以下のようなエラーが発生しました。

remote: -----> Installing node-v6.11.1-linux-x64
remote: -----> Installing yarn-0.22.0
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
remote:        Webpacker requires Yarn >= 0.25.2 and you are using 0.22.0
remote:        Please upgrade Yarn https://yarnpkg.com/lang/en/docs/install/
remote:  !
remote:  !     Precompiling assets failed.
remote:  !
remote:  !     Push rejected, failed to compile Ruby app.
remote:
remote:  !     Push failed

今回はこの問題についての対応方法です。

バージョン

rails: 5.1.0
webpacker: 3.0.1

原因

webpacker3.0.1では0.25.2以上のバージョンのyarnが必要なのですが、Heroku側には0.22.0がインストールされているためエラーが出ているようです。
Heroku側で0.22.0がインストールされてしまう原因は、こちらのコメントにありますが、heroku-buildpack-ruby(Heroku側でRailsアプリをビルドしてくれるツール)でyarnのバージョンがハードコードされているためです(該当コード)。2017年9月19日現在、そのバージョンを上げるPRが上がっています。マージされれば、上記の問題も起こらなくなると思われます。

対応

Heroku公式にありますが、heroku-buildpacksが nodejs -> ruby の順で実行されるようにすればデプロイできました。コマンドだと

$ heroku buildpacks:clear -a (アプリ名) # buildpacks を初期化する
$ heroku buildpacks:add heroku/nodejs -a (アプリ名)
$ heroku buildpacks:add heroku/ruby -a (アプリ名)

になります。これで、heroku-buildpack-nodejs側でyarnがインストールされました(バージョンは0.28.4でした)。
なお、バージョンを指定させたい時にはアプリケーションの package.json に

package.json
"engines": {
  "yarn": ">= 0.25.2"
}

のように書くとheroku-buildpack-nodejs側でバージョンを指定してインストールしてくれるようです。

3
1
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?