Last updated: Apr 4, 2024
Reading time·4 min
The default port for an Angular application is 4200.
When you start your server with npm start
, if there is another application
running on port 4200, you get a message "Port 4200 is already in use. Would
you like to use a different port? (Y/n)".
npm run start > bobbyhadz-angular@0.0.0 start > ng serve ? Port 4200 is already in use. Would you like to use a different port? (Y/n)
--port
flagYou can change your default Angular port by setting the --port
flag when
issuing the ng serve
command, e.g. ng serve --port 3456
.
You can add the --port
flag to the ng serve
command in the scripts
section of your package.json
file.
Here is the updated start
script with the PORT number changed.
ng serve --port 3456
And here is the entire scripts
section of my package.json
file.
{ "scripts": { "ng": "ng", "start": "ng serve --port 3456", "build": "ng build", "watch": "ng build --watch --configuration development", "test": "ng test" }, }
Now I can start my Angular server with npm start
and it will run on
http://localhost:3456/.
You don't necessarily have to set the port to 3456
, you can pick any other
port, e.g. 4567
.
package.json
If you don't want to change the start
script in your package.json
file, run
the ng serve --port 3456
command directly from your terminal.
ng serve --port 3456
Make sure you have the Angular CLI installed to be able to run the command.
npm install -g @angular/cli
Alternatively, you can change the port directly in your angular.json
file.
angular.json
To change the default port of your Angular application in your angular.json
file:
angular.json
file that is located in the root directory of your
project (next to package.json
).architect
-> serve
, create an options
object and set the port
number.{ "$schema": "./node_modules/@angular/cli/lib/config/schema.json", "projects": { "bobbyhadz-angular": { "architect": { "serve": { "builder": "@angular-devkit/build-angular:dev-server", "options": { "port": 3456 }, } } } } }
Here is a screenshot that demonstrates the change.
You simply have to add the options
object under serve
and set the port
to
a different value, e.g. 3456
.
Now if I start my angular application with npm start
, it opens on port 3456
and is available at http://localhost:3456/.
If you get the message "Port 4200 is already in use. Would you like to use a
different port? (Y/n)" and type Y
, the Angular server opens on a different
port.
npm start
Port 4200 is already taken in the example, so the Angular server started on port
34611
but this could be any other port.
If you need to identify the port your server started on, look at the message in your terminal.
An alternative approach is to stop the process that runs on port 4200 (the default Angular port).
If you need to stop the process that runs on port 4200 to be able to start your Angular app on the port, use the kill-port npm package.
npx kill-port 4200
After you stop the process that runs on port 4200, you will be able to start your Angular server using the default port.
npm start
You can also install the package globally instead of using npx
.
# install the package npm install --global kill-port # stop the process on port 4200 kill-port --port 4200
After you stop the process that runs on port 4200, you will be able to start your Angular app on the default port.
You can learn more about the related topics by checking out the following tutorials: