简体   繁体   中英

nodemon starts the server with an error every second time after saving

I am using linux mint 19.02 . Wrote a simple server.

const http = require('http');
const express = require('express');
const dotenv = require('dotenv').config();

const app = express();
const server = http.createServer(app);
const port = process.env.PORT // 8081;

app.get('/', (req, res) => {
    res.send('Hello world!!!');
});

Here are my package.json file.

{
  "name": "matel",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node src/index.js",
    "dev": "nodemon src/index.js"
  },
  ...
  "license": "ISC",
  "dependencies": {
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "nodemon": "^1.19.4"
  }
}

When I start the server with the npm run dev command, I get an error. And by clicking on the link http://localhost:8081/ in the browser, I also get an error (logical).

events.js:187 throw er; // Unhandled 'error' event ^

Error: listen EADDRINUSE: address already in use:::8081 at Server.setupListenHandle [as _listen2] (net.js:1300:14) at listenInCluster (net.js:1348:12) at Server.listen (net.js:1436:7) at Object. (/home/user/projects/matel/src/index.js:13:8) at Module._compile (internal/modules/cjs/loader.js:956:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10) at Module.load (internal/modules/cjs/loader.js:812:32) at Function.Module._load (internal/modules/cjs/loader.js:724:14) at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10) at internal/main/run_main_module.js:17:11 Emitted 'error' event on Server instance at: at emitErrorNT (net.js:1327:8) at processTicksAndRejections (internal/process/task_queues.js:80:21) { code: 'EADDRINUSE', errno: 'EADDRINUSE', syscall: 'listen', address: '::', port: 8081 }

But when I click save ctrl + s key file, everything works fine. And by clicking on the link http://localhost:8081 / in the browser, I get Hello world!!! , When I press the key combination ctrl + s again, I get an error again. And so every second time.

I ran the fuser -k 8081/tcp command, after which the server started without error, but when I save the file, I get an error, the next time the server is saved, it starts working. And so every second time.

What could be the reason that the server might behave this way? What's my mistake?

So based on the error message(And its frequency of occurring), I think that the problem here is that the port could be used by some other application. Ports 8080 and 8081 are probably commonly used and the goto ports for applications like Skype, etc. So when you get it to work you're probably hijacking the port for a bit. So it would be like a whole power struggle between the different applications and that port.

So I suggest trying different ports and seeing if that doesn't fix the problem?

you can use

fuser -n tcp -k portnumber

I think nodemon is unable to restart the application before it is closed in the previous session. Try to put a command to close the previous session as the restart happens. pstree is not returning all the process ids to kill when restarting the server.

Re: Update pstree.remy and restart the pc. (Sovled for me): sudo npm intall -g pstree.remy OR Revert to an old version. (also worked): npm i pstree.remy@1.1.0

Open your terminal and execute killall node . This should solve your problem.

killall - will kill processes by name, so in this case, it will kill node processes.

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