【问题标题】:How can you fix a 'port already in use' error while trying to run a Rails + React app locally on a Mac?尝试在 Mac 上本地运行 Rails + React 应用程序时,如何修复“端口已在使用”错误?
【发布时间】:2020-06-20 16:39:31
【问题描述】:

我正在开发基于 this 的示例 react/rails 应用程序。 在出现此问题前几天它运行良好,但我不知道是什么原因造成的,也不知道如何解决。

对于我尝试在其上运行 Web 服务器的任何端口,我都会遇到这种情况。

这种事情列出没有要杀死的进程:lsof -nP -iTCP:3000| grep LISTEN

这也显示没有结果:lsof -i tcp:3000

似乎 react 开始正常(在 3000 上),然后 rails 开始(在 3001 上)然后有某种碰撞导致它关闭。

这是我正在使用的 Procfile.dev:

web: PORT=3000 yarn --cwd client run start
api: PORT=3001 bundle exec rails s

react 应用程序位于 rails 应用程序的 /client 目录中。

这是来自 React 应用的 package.json 的代理行:"proxy": "http://localhost:3001/",

这是终端输出:

$ bin/rake start
Running via Spring preloader in process 41869
[OKAY] Loaded ENV .env File as KEY=VALUE Format
12:38:45 PM web.1 |  yarn run v1.22.4
12:38:45 PM web.1 |  $ react-scripts start
12:38:46 PM api.1 |  => Booting Puma
12:38:46 PM api.1 |  => Rails 6.0.3.1 application starting in development 
12:38:46 PM api.1 |  => Run `rails server --help` for more startup options
12:38:46 PM web.1 |  Something is already running on port 3000.
12:38:46 PM web.1 |  Done in 1.06s.
[DONE] Killing all processes with signal  SIGINT
12:38:46 PM web.1 Exited Successfully
12:38:46 PM api.1 |  Exiting
12:38:46 PM api.1 Exited Successfully

rake 任务 (lib/tasks/start.rake) 是:

namespace :start do
  task :development do
    exec 'heroku local -f Procfile.dev'
  end
end

desc 'Start development server'
task :start => 'start:development'

感谢观看!

【问题讨论】:

  • 运行这个lsof -i tcp:3000命令,然后跟踪所有运行在3000端口的进程,然后一个一个kill掉。
  • 这不显示任何结果。我过去曾将其用于其他类似问题,但这不是这里的问题。

标签: ruby-on-rails reactjs port


【解决方案1】:

在运行npm start之前执行export PORT = 'portNumber' (不带引号)

【讨论】:

    【解决方案2】:

    我最终只是擦除了 repo 并克隆了旧版本。现在一切正常,我只是失去了一天的工作。我认为这对我来说已经足够了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-09
      • 1970-01-01
      • 2018-10-23
      • 2013-04-29
      • 2019-07-07
      • 2020-02-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多