ng serve --open reports port 4200 is already in us

2020-07-30 00:10发布

问题:

When I try to run my angular app using command

ng serve -o --port 4200

it shows the error like,

Port 4200 is already in use , try different port

But I checked that, there is no running process in that port using this command

sudo lsof -i -P -n | grep LISTEN

I have also tried different ports but result is same showing that port is also in use.

When I tried dynamic port selection command ( --port 0 ) the app runs . But I must have to specify the port number.

I am using
Node version - 8.12.0
Angular version - 5.2.6
Angular CLI version - 1.7.1

回答1:

Had the same problem, it's a bad version of portfinder. Until they fix it, specify directly in devDependences:

"portfinder": "1.0.20",


回答2:

If you get this error: ‘Port 4200 is already in use. Use ‘–port’ to specify a different port’, you might have left your Angular development environment without closing the node.js webserver’s port.

Here’s how you can close it without having to reboot your computer or change your application’s port.

Step 1: Find the connection’s PID

netstat -ano | findstr :yourPortNumber

Example with its output:

netstat -ano | findstr :4200

and output is :

TCP   127.0.0.1:4200    0.0.0.0:0    LISTENING    10764

Step 2: Kill the process using it’s PID

tskill yourPID

Example:

tskill 10764

Step 3: Restart your server

You should be able to run it (using ng serve)

Step 4: Stop your server properly

Don’t forget now to close properly your server by using Ctrl + C and typing Y:

That’s it.



回答3:

Try to remove --port 4200. By default angular cli will serve your angular app in port 4200.



回答4:

Odds are you have a server running somewhere. This can happen when you quit a terminal window without stopping the server, for example. Open a task manager, and kill any Node processes. That should clear things up. Restarting your computer should accomplish the same thing.