Questions & Answers

"Port 4200 is already in use. Use '--port' to specify a different port."

Hey i am runnning an angular2 app with express as backend. Getting the following issue:

[3] Port 4200 is already in use. Use '--port' to specify a different port.
[3] [nodemon] app crashed - waiting for file changes before starting...
[1] [HPM] Error occurred while trying to proxy request /api/jobs from localhost:4200 to http://localhost:3000 (ECONNREFUSED) (


import * as bodyParser from 'body-parser';
import * as dotenv from 'dotenv';
import * as express from 'express';
import * as morgan from 'morgan';
import * as mongoose from 'mongoose';
import * as path from 'path';

import setRoutes from './routes';

const app = express();
dotenv.load({path: '.env'});
app.set('port', (process.env.PORT || 3000));

app.use('/', express.static(path.join(__dirname, '../')));

app.use(bodyParser.urlencoded({extended: false}));


const db = mongoose.connection;
(<any>mongoose).Promise = global.Promise;

db.on('error', console.error.bind(console, 'connection error:'));
db.on('open', () => {
  console.log('Connected to MongoDB');

  app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname, '../index.html'));

  app.listen(app.get('port'), () => {
    console.log('Angular 2 Full Stack listening on port ' + 

export {app};

edit: Here the hole code. I think i dont reach the line with db.on('open')

when i inspect the port:

I using concurrently: with this options

"dev": "concurrently \"mongod\" \"ng serve -pc proxy.conf.json --open\" \"tsc -w -p server\" \"nodemon dist/server/server.js\"",
2023-01-07 20:31:05
are you running this locally or on a linux box?
2023-01-07 20:31:05
I use it locally
2023-01-07 20:31:05
what port is your mongo running on locally?
2023-01-07 20:31:05
Standard one 27017
2023-01-07 20:31:05
let me know if that answer works
Answers(5) :

Looks like there is some process running locally on 4200. Here is what I would do-

  1. Execute ps -ef | grep 4200 on your terminal to check which process is running locally
  2. Kill that process if thats not useful. kill -9 ${Process_Id} This will force kill the process.
  3. Try to run the app again.
2023-01-07 20:31:05
Didn't worked. I think the node app wants to use the same port as the angular one. But i specify it ass 3000 and not 4200
2023-01-07 20:31:05
well thats partially true. You do this in your node app- app.set('port', (process.env.PORT || 3000)); which means if you have PORT variable set in your bash profile to point to 4200 it will take that address. I would say try removing process.env.port OR condition in that command and see if node app runs on 3000
2023-01-07 20:31:05
First thanks for your help but still no solution. Have postetd the hole server.ts on top. I don't think i reach the line db.on('open')
2023-01-07 20:31:05
Hey don't ask me how, but it is working... ty for your time and help.

webpack: Compiled successfully. ^C [1]+ Killed: 9 ng serve

webpack: Compiled successfully. ^Z [1]+ Stopped ng serve

If you use Ctrl+Z in Mac you will have ´Port 4200 is already in use. Use '--port' to specify a different port.´later.

That means the port have running some other process so you have two solution.

1 run the angular in different port by

ng serve --port '{new port}'

2 kill the same port and serve it in same port

sudo kill $(sudo lsof -t -i:4200)
ng serve

For Windows it might be poosible that the port is already running in that case go to cmd as administrator and look up PID for the running port. For example my running port is 4200.

Then run this command on cmd

netstat -a -n -o

Find port with port number 4200 by lookingup in the list or you can right click on terminal and click find here enter 4200 in "find what" and click "find next": For example you found that port number 4200 is used by pid 17200 then type below command in cmd:

taskkill -f /pid 17200

and then you can link your port or start serve command as required