简体   繁体   中英

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 . 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.

This kind of thing lists no processes to kill: lsof -nP -iTCP:3000| grep LISTEN lsof -nP -iTCP:3000| grep LISTEN

This also shows no results: 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.

Here is the Procfile.dev I am using:

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.

Here is the proxy line from the react app's package.json : "proxy": "http://localhost:3001/",

Here is the terminal 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:

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

I ended up just wiping the repo and cloning the older version. Now everything works fine, I just lost like a day of work. I think that is good enough for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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