angular-cli: ng serve --open reports port 4200 is already in use when it isn't

ng serve --open incorrectly reports that port 4200 is in use, even when no browsers are open.

Versions

Angular CLI: 1.6.7
Node: 8.9.4
OS: win32 x64
Angular: 5.2.3
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.6.7
@angular-devkit/build-optimizer: 0.0.42
@angular-devkit/core: 0.0.29
@angular-devkit/schematics: 0.0.52
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.9.7
@schematics/angular: 0.1.17
typescript: 2.5.3
webpack: 3.10.0

Repro steps

  • ng new aatest1 -si
  • cd aatest1
  • npm install
  • ng serve --open

Observed behavior

ps>ng serve --open
Port 4200 is already in use. Use '--port' to specify a different port.

Desired behavior

ng serve --open should work correctly

Mention any other details that might be useful (optional)

The above behavior did NOT occur with Angular-CLI 1.6.6 or earlier.

Work-around

specify --port 4216 (any value between 4200 and 4215 causes the above mentioned error)

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 18 (1 by maintainers)

Most upvoted comments

fuser -n tcp -k 4200 ng serve

ps>sl aaTest1
ps>ng serve --open
Port 4200 is already in use. Use '--port' to specify a different port.
ps>netstat -abno | out-file "$env:temp\aatest.txt" -Force -Width 1024 -encoding ASCII
ps>npp "$env:temp\aatest.txt"
ps>

I have attached the output from netstat. I cannot find anything that is listening on ports 4200 through 4215, the ones that no longer work as of Angular-CLI 1.6.7.

It’s as if the parsing of the default port number’s last two digits (00) is messed up because specifying port 4216 or above seems to work.

If I specify port 4201, for example, I still get the same error (port 4201 is in use):

ng serve --open --port=4201

Only ports 4216 and above work for me. Example:

ng serve --open --port=4216

aatest.txt

If you are receiving that error, something on your system is listening on that port. If on Windows, you can use netstat -abno to find which process is listening on the port(s) in question.

I confirm that the error is present in Windows from version 1.6.7 and later. It occurs every time the application build stops due to some errors. Evidently in case of an error the used door is not closed. What instead happens correctly until version 1.6.6

Until this issue is resolved, Windows users can run the following PowerShell script that will find and kill the process that is still using Port 4200:

<#
  Purpose: Kills the process that is listening on the specified port (default port is 4200)
  Author: Fred Morrison
  Date: 2018-02-18
  Intended Purpose: Overcome bug in Angular-CLI 1.7.0 that fails to fully kill the process that it started via:
                    ng start --open 
                    even when the user thinks it has been killed in their command/powershell prompt.

#>
param([int] $port = 4200)
# Remember user's current preference
$previousVerbosePreference = $VerbosePreference
if($previousVerbosePreference -ne 'Continue')
{
  $VerbosePreference = 'Continue'
}
[string] $cmd = '$a = NETSTAT.EXE -a -o -n'
Write-Verbose -Message $cmd
Invoke-Expression -Command $cmd

$p = $a.trim() | 
  Select-Object -Skip 4 | 
  ConvertFrom-String -PropertyNames Protocol, LocalAddress, RemoteAddress, State, PID
#$p | Format-Table
$portToKill = $p | 
  Where-Object -Property LocalAddress -EQ -Value ('127.0.0.1:{0}' -f $port) |
  Where-Object -Property State -EQ -Value 'LISTENING'

if($portToKill -eq $null)
{
  Write-Verbose -Message ('port {0} is not in use. No action taken.' -f $port)
}
else 
{
  $pidToKill = $portToKill.PID
  $cmd = "Taskkill /F /PID $pidToKill"
  Write-Verbose -Message $cmd
  Invoke-Command -ScriptBlock ([ScriptBlock]::Create($cmd))
}
# restore user's preference
$VerbosePreference = $previousVerbosePreference

Example output when port 4200 is being used:

VERBOSE: $a = NETSTAT.EXE -a -o -n
VERBOSE: Taskkill /F /PID 12176
SUCCESS: The process with PID 12176 has been terminated.

Example output when port 4200 is not being used:

VERBOSE: $a = NETSTAT.EXE -a -o -n
VERBOSE: port 4200 is not in use. No action taken.

Ubuntu ( terminal command ) kill -9 $(lsof -i tcp:4200 -t)

On a mac: netstat -anv | grep 4200

@gmarab : correct. It is still happening to me with Angular-CLI 1.7.0.

I have to “fight the tool” (Angular-CLI in this case) by constantly changing the --port value if an error occurs and I have to start the process up again:

ng serve --open  --port=4201 --vendor-chunk --extract-css --common-chunk --named-chunks --output-hashing=none --progress

It’s annoying and I hope this gets fixed soon.