症状
RailsAPIモードとReactを使用中に、npmstartとRailsサーバーを起動しようとすると、Railsサーバーを起動したターミナルに下記のエラーメッセージが表示されてしまいました。ターミナル
rails s
=> Booting Puma
=> Rails 6.0.3.6 application starting in development
=> Run `rails server --help` for more startup options
Puma starting in single mode...
* Version 4.3.7 (ruby 2.6.3-p62), codename: Mysterious Traveller
* Min threads: 5, max threads: 5
* Environment: development
Exiting
Traceback (most recent call last):
40: from bin/rails:3:in `<main>'
39: from bin/rails:3:in `load'
38: from /home/ubuntu/environment/プロジェクト名/bin/spring:15:in `<top (required)>'
37: from /home/ubuntu/environment/プロジェクト名/bin/spring:15:in `require'
36: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `<top (required)>'
35: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/binstub.rb:11:in `load'
34: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/bin/spring:49:in `<top (required)>'
33: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/client.rb:30:in `run'
32: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/client/command.rb:7:in `call'
31: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `call'
30: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/spring-2.1.1/lib/spring/client/rails.rb:28:in `load'
29: from /home/ubuntu/environment/プロジェクト名/bin/rails:9:in `<top (required)>'
28: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:31:in `require'
27: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:22:in `require_with_bootsnap_lfi'
26: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/loaded_features_index.rb:92:in `register'
25: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `block in require_with_bootsnap_lfi'
24: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/bootsnap-1.7.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:23:in `require'
23: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/railties-6.0.3.6/lib/rails/commands.rb:18:in `<main>'
22: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/railties-6.0.3.6/lib/rails/command.rb:46:in `invoke'
21: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/railties-6.0.3.6/lib/rails/command/base.rb:69:in `perform'
20: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/thor-1.1.0/lib/thor.rb:392:in `dispatch'
19: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/thor-1.1.0/lib/thor/invocation.rb:127:in `invoke_command'
18: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/thor-1.1.0/lib/thor/command.rb:27:in `run'
17: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/railties-6.0.3.6/lib/rails/commands/server/server_command.rb:138:in `perform'
16: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/railties-6.0.3.6/lib/rails/commands/server/server_command.rb:138:in `tap'
15: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/railties-6.0.3.6/lib/rails/commands/server/server_command.rb:147:in `block in perform'
14: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/railties-6.0.3.6/lib/rails/commands/server/server_command.rb:39:in `start'
13: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/rack-2.2.3/lib/rack/server.rb:327:in `start'
12: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/rack/handler/puma.rb:73:in `run'
11: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/launcher.rb:172:in `run'
10: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/single.rb:98:in `run'
9: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/runner.rb:161:in `load_and_bind'
8: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/binder.rb:90:in `parse'
7: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/binder.rb:90:in `each'
6: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/binder.rb:106:in `block in parse'
5: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/binder.rb:222:in `add_tcp_listener'
4: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/binder.rb:222:in `each'
3: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/binder.rb:223:in `block in add_tcp_listener'
2: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/binder.rb:229:in `add_tcp_listener'
1: from /home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/binder.rb:229:in `new'
/home/ubuntu/.rvm/gems/ruby-2.6.3/gems/puma-4.3.7/lib/puma/binder.rb:229:in `initialize': Address already in use - bind(2) for "127.0.0.1" port 8080 (Errno::EADDRINUSE)
最後の行にあるエラーメッセージを翻訳すると「`initialize ':アドレスはすでに使用されています」でした。
解決方法
npmのほうでも、同様のポート番号を使っていたようです。npmでは起動時にすでにポートを使用していると、違うポートを使うかを聞いてくるので、Railsサーバーを起動後にnpmstartを実施したら、エラーが表示されなくなりました。
rails側でポート番号を変更する方法についてはこちらに記載があったので、ご参考ください。
https://qiita.com/Tocyuki/items/de66987ead2183e4fcae
参考
rails sが立ち上がらない・・・ https://qiita.com/kamyu/items/1d39d4e57f04a3ad6b0cRailsでポートとIPアドレスを指定する方法
https://qiita.com/Tocyuki/items/de66987ead2183e4fcae