0

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) (https://nodejs.org/api/errors.html#errors_common_system_errors)

server.js:

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.json());
app.use(bodyParser.urlencoded({extended: false}));

app.use(morgan('dev'));

mongoose.connect(process.env.MONGODB_URI);
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');
  setRoutes(app);

  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 ' + 
  app.get('port'));
  });
});

export {app};

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

when i inspect the port:

https://i.stack.imgur.com/l81lJ.png

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\"",
onetwo12
  • 2,359
  • 5
  • 23
  • 34
Dani R
  • 551
  • 1
  • 4
  • 15

5 Answers5

0

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.
Rohan Rayarikar
  • 240
  • 2
  • 6
  • 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 – Dani R Jun 10 '17 at 15:39
  • 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 – Rohan Rayarikar Jun 10 '17 at 15:43
  • 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')` – Dani R Jun 10 '17 at 16:38
  • Hey don't ask me how, but it is working... ty for your time and help. – Dani R Jun 10 '17 at 17:08
0

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.

onetwo12
  • 2,359
  • 5
  • 23
  • 34
0

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
hazan kazim
  • 938
  • 9
  • 11
0

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

Dharman
  • 30,962
  • 25
  • 85
  • 135
Rashi Goyal
  • 933
  • 9
  • 15
0

Use npx kill-port 4200 then use ng serve

Reference: “Port 4200 is already in use”. Killing all the processes associated with 4200 did not work.

AlanCan
  • 21
  • 5