I use Ubuntu 20.04, Nodemon 2.0.4, Node 14.9.0 and Express 4.17.1. Every time I save my code I get this error:
events.js:291
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE: address already in use :::3000
at Server.setupListenHandle [as _listen2] (net.js:1318:16)
at listenInCluster (net.js:1366:12)
at Server.listen (net.js:1452:7)
at Function.listen (/home/ubuntu/kopum/node_modules/express/lib/application.js:618:24)
at Object.<anonymous> (/home/ubuntu/kopum/app.js:37:5)
at Module._compile (internal/modules/cjs/loader.js:1075:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1096:10)
at Module.load (internal/modules/cjs/loader.js:940:32)
at Function.Module._load (internal/modules/cjs/loader.js:781:14)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at internal/main/run_main_module.js:17:47
Emitted 'error' event on Server instance at:
at emitErrorNT (net.js:1345:8)
at processTicksAndRejections (internal/process/task_queues.js:80:21) {
code: 'EADDRINUSE',
errno: -98,
syscall: 'listen',
address: '::',
port: 3000
}
[nodemon] app crashed - waiting for file changes before starting...
And then I try to run this on terminal:
sudo lsof -i :3000
kill -9 {PID}
Then I restart the Nodemon by running nodemon app.js
and everything's back to normal. But the EADDRINUSE is back again when I add some code and save it.
A simple fix that worked when I had a similar issue is to delay Nodemon's restart slightly:
nodemon --delay 500ms app.js
This seems to give a little time for ports to be correctly released.
Seems like nodemon
does not correctly kill child-spawn processes before start;
Edit the scripts
attribute in your package.json
file and add a kill command to terminate any processes listening on your port before the server starts.
"scripts": {
"start": "npm run kill && nodemon app.js",
"kill": "kill -9 $(lsof -i :3000) &>/dev/null | exit 0"
}
尝试在每次保存命令后运行 npx kill-port 3000
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.