LoginSignup
9
6

More than 1 year has passed since last update.

[2021年]herokuでrails + nodejsでnodeのバージョン指定する方法

Last updated at Posted at 2021-07-14

前置き

railsアプリを構築し、webpacker用にnodejsを入れたアプリを作った際、
nodeのバージョンが12系なのが気になった。
現時点でのLTSは14系なので開発環境と差異が発生しており環境差異でハマるのが嫌なのでheroku側のnodeバージョンを上げる方法を追求してみた。

軽く検索するとよく引っかかるのが heroku Buildpacks を使うというもの。

公式ドキュメント

だがしかし、上記を見てもバージョンを指定する具体的な方法自体は記載がない。

ググって見た感じ、 package.json にバージョンを追記するとバージョンが指定できるとある。

  "engines": {
    "node": "14.17.2",
    "npm": "6.14.13"
  }

ただし、railsアプリでこれをやってpushしても

remote:        error app@1.0.0: The engine "node" is incompatible with this module. Expected version "14.17.2". Got "12.16.2"
remote:        error Found incompatible module.

と、出る。

結論

前置きが長くなったが結論。
上記が出るのにはいくつか原因がある。

$ heroku buildpacks
=== dev-playrooms-free Buildpack URLs
1. heroku/ruby
2. heroku/nodejs

heroku buildpacks このコマンドを打って

  • 2にnodeが来ている
  • そもそもnodejsが無い

上記のパターンでデプロイに失敗する。

解決方法

  • Herokuのアプリ設定を開く
  • Settingsタブを開く
  • Buildpacksの項目で add buildpacks でnodejsを追加する
  • ここの順番がデプロイの順番に影響するのでnodejsを一番上に持ってくる

上記のあと、デプロイすればnodejsのバージョンがpackage.jsonで指定したものにアップグレードされるようだ。

余談

remote:        engines.node (package.json):  14.17.2
remote:        engines.npm (package.json):   6.14.13
remote:        engines.yarn (package.json):  unspecified (use default)

enginesでnode, npm, yarnのバージョンが指定できるようだ。

その後、ローカルのdocker buildでnodeのバージョンが上がってyarnがエラーをはいた。
よって下記のように設定した。

  "engines": {
    "node": "14.x",
    "npm": "6.x",
    "yarn": "1.x"
  }
9
6
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
9
6