気ままに気ままのエンジニアブログ

定期的に得た知見を気ままに発信中

【Rails/環境構築(M1)】Could not find a JavaScript runtime. (ExecJS::RuntimeUnavailable)エラーの対処方法

こんにちは。

最近SQLにハマってます。

どうもハチマキです。

はじめに

最近MacBook Air(M1,2020)を購入したため、開発環境の構築を行いました。その時に発生したエラーの対処方法についてです。

早速結論になりますが、この記事で対象にしているエラーExecJS::RuntimeUnavailableですが、下記記載した解消方法では根本的に解消できませんでした(サーバ起動時にエラーが発生する)

ですので、もしこの記事で書かれた内容で解消できない場合は、こちらの記事と合わせて参考にして頂けばと思います。
【Rails/環境構築(M1)】ローカルサーバ起動後にアクセスすると「dyld: lazy symbol binding failed: Symbol not found」エラーが発生し、サーバが落ちる時の対処方法 - めがね屋のエンジニアブログ

環境

問題

rspecのテストコマンドを実行すると、JS関連でエラーが発生する。

% bundle exec rspec

An error occurred while loading ./spec/features/~.rb
Failure/Error: require File.expand_path('../config/environment', __dir__)

ExecJS::RuntimeUnavailable:
  Could not find a JavaScript runtime. See https://github.com/rails/execjs for a list of available runtimes.
# ./vendor/ruby/2.6.0/gems/execjs-2.7.0/lib/execjs/runtimes.rb:58:in `autodetect'
# ./vendor/ruby/2.6.0/gems/execjs-2.7.0/lib/execjs.rb:5:in `<module:ExecJS>'
# ./vendor/ruby/2.6.0/gems/execjs-2.7.0/lib/execjs.rb:4:in `<main>'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
# ./vendor/ruby/2.6.0/gems/uglifier-4.2.0/lib/uglifier.rb:5:in `<main>'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
# ./vendor/ruby/2.6.0/gems/bootsnap-1.7.1/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
# ./config/application.rb:7:in `<top (required)>'
# ./config/environment.rb:2:in `require_relative'
# ./config/environment.rb:2:in `<top (required)>'
# ./spec/rails_helper.rb:5:in `require'
# ./spec/rails_helper.rb:5:in `<top (required)>'
# ./spec/features/~.rb:1:in `require'
# ./spec/features/~.rb:1:in `<top (required)>'
No examples found.


Finished in 0.00004 seconds (files took 3.01 seconds to load)
0 examples, 0 failures, 1 error occurred outside of examples

原因

Javascriptを実行するために必要なランタイム(ソフトウェアの実行に必要なプログラム)がないことでエラーが発生

解決方法

必要なランタイムをインストールすれば解消です。
rails newで生成されたGemfileのmini_racerコメントアウトを外し、bundle installを行うことで解消できます。

Gemfileファイル

#gem 'mini_racer', platforms: :ruby #コメントアウト外す
gem 'mini_racer', platforms: :ruby

ターミナル(コメントアウト外した後に)

% bundle install

*追記(20210317)
結局この対応方法では解消できませんでした。
解消するためにやったことを簡潔に申しますと、mini_racerの代わりとなる必要なランタイムをインストールする対応です。

▼こちらの記事も合わせて、ご参考にしてください。
【Rails/環境構築(M1)】ローカルサーバ起動後にアクセスすると「dyld: lazy symbol binding failed: Symbol not found」エラーが発生し、サーバが落ちる時の対処方法 - めがね屋のエンジニアブログ