简体   繁体   English

尝试在 Mac 上本地运行 Rails + React 应用程序时,如何修复“端口已在使用”错误?

[英]How can you fix a 'port already in use' error while trying to run a Rails + React app locally on a Mac?

I am working on a sample react/rails app based on this .我正在开发一个基于的示例 react/rails 应用程序。 It was working fine for a few days before this issue arose, and I can't figure out what caused it or how to fix it.在出现此问题之前几天它工作正常,我无法弄清楚是什么原因导致它或如何解决它。

I get this behavior for any port I try to run the web server on.对于我尝试在其上运行 web 服务器的任何端口,我都会得到这种行为。

This kind of thing lists no processes to kill: lsof -nP -iTCP:3000| grep LISTEN这种事情没有列出要杀死的进程: lsof -nP -iTCP:3000| grep LISTEN lsof -nP -iTCP:3000| grep LISTEN

This also shows no results: lsof -i tcp:3000这也显示没有结果: lsof -i tcp:3000

It seems like react starts fine (on 3000), then rails starts (on 3001) then there is a collision of some kind causing it to shut down.似乎反应开始正常(在 3000 上),然后 rails 开始(在 3001 上)然后有某种碰撞导致它关闭。

Here is the Procfile.dev I am using:这是我正在使用的 Procfile.dev:

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

The react app lives in the /client directory within the rails app. react 应用程序位于 rails 应用程序的/client目录中。

Here is the proxy line from the react app's package.json : "proxy": "http://localhost:3001/",这是来自反应应用程序package.json的代理行: "proxy": "http://localhost:3001/",

Here is the terminal output:这是终端output:

$ 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

The rake task ( lib/tasks/start.rake ) is: 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'

Thanks for taking a look!谢谢参观!

do export PORT = 'portNumber' (without the quotes) just before you run npm start在运行npm start之前执行export PORT = 'portNumber' (不带引号)

I ended up just wiping the repo and cloning the older version.我最终只是擦除了 repo 并克隆了旧版本。 Now everything works fine, I just lost like a day of work.现在一切正常,我只是失去了一天的工作。 I think that is good enough for me.我认为这对我来说已经足够好了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM