简体   繁体   中英

App crash when npm start using express and nodemon

I have a simple app.js -

const express = require('express');

const app = express();

//Route
app.get('/', (req, res) => {
    res.send('Welcome to custom api');
})

//Listen
app.listen(3000);

My package.json looks like this -

{
  "name": "custom-api",
  "version": "1.0.0",
  "description": "\"API to give custom info\"",
  "main": "index.js",
  "scripts": {
    "start": "nodemon app.js"
  },
  "license": "Apache-2.0",
  "dependencies": {
    "express": "^4.17.1",
    "nodemon": "^2.0.2"
  }
}

I run npm start and get this error -

> custom-api@1.0.0 start /Users/workspace/server
> nodemon app.js

[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node app.js`
events.js:187
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1301:14)
    at listenInCluster (net.js:1349:12)
    at Server.listen (net.js:1437:7)
    at Function.listen (/Users/workspace/server/node_modules/express/lib/application.js:618:24)
    at Object.<anonymous> (/Users/workspace/server/app.js:11:5)
    at Module._compile (internal/modules/cjs/loader.js:959:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:995:10)
    at Module.load (internal/modules/cjs/loader.js:815:32)
    at Function.Module._load (internal/modules/cjs/loader.js:727:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1047:10)
Emitted 'error' event on Server instance at:
    at emitErrorNT (net.js:1328:8)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  code: 'EADDRINUSE',
  errno: 'EADDRINUSE',
  syscall: 'listen',
  address: '::',
  port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...

Following error means 3000 is in already use, may be your ran it from another terminal or what or may be some other app is using it.

Error: listen EADDRINUSE: address already in use :::3000

So try some other port like 3001 or 4000 to see if that works.

Then you can find the process that's already using 3000 and kill it, if it makes sense.

Error: listen EADDRINUSE: address already in use :::3000

Port 3000 is already in use. Either another program is using the port (in which case you'll have to pick another port), or you failed to terminate this program correctly last time you ran it. If you're on linux run ps and find the process that was started by the command nodemon app.js and copy its pid . Then you terminate that process by running kill followed by the pid that you just copied.

Ex:

$ ps
  PID TTY          TIME CMD
    8 tty1     00:00:00 bash
   32 tty1     00:00:00 nodemon app.js
   33 tty1     00:00:00 ps

$ kill 32

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM