错误:使用带有 babel 7 的 nodemon 时监听 EADDRINUSE

Posted

技术标签:

【中文标题】错误:使用带有 babel 7 的 nodemon 时监听 EADDRINUSE【英文标题】:Error: listen EADDRINUSE when using nodemon with babel 7 【发布时间】:2019-04-29 21:02:22 【问题描述】: nodemon -v:1.18.6 节点-v:v11.2.0 命令:nodemon --exec babel-node lib/server.js

我的应用程序可以使用上述命令启动。当我更改源代码时,我希望 nodemon 会自动重启。但我收到了错误Error: listen EADDRINUSE: address already in use :::3000

错误详情

[nodemon] starting `babel-node lib/server.js`
events.js:167
      throw er; // Unhandled 'error' event
      ^

Error: listen EADDRINUSE: address already in use :::3000
    at Server.setupListenHandle [as _listen2] (net.js:1294:14)
    at listenInCluster (net.js:1342:12)
    at Server.listen (net.js:1429:7)
    at Function.listen (/Users/longntran/Desktop/learning/pangara-web/node_modules/express/lib/application.js:618:24)
    at Object.listen (/Users/longntran/Desktop/learning/pangara-web/lib/server.js:12:5)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Module._compile (/Users/longntran/Desktop/learning/pangara-web/node_modules/pirates/lib/index.js:83:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Object.newLoader [as .js] (/Users/longntran/Desktop/learning/pangara-web/node_modules/pirates/lib/index.js:88:7)
    at Module.load (internal/modules/cjs/loader.js:620:32)
Emitted 'error' event at:
    at emitErrorNT (net.js:1321:8)
    at internalTickCallback (internal/process/next_tick.js:72:19)
    at process._tickCallback (internal/process/next_tick.js:47:5)
    at Function.Module.runMain (internal/modules/cjs/loader.js:778:11)
    at Object.<anonymous> (/Users/longntran/Desktop/learning/pangara-web/node_modules/@babel/node/lib/_babel-node.js:224:23)
    at Module._compile (internal/modules/cjs/loader.js:722:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3) 

依赖关系

"dependencies": 
    "ejs": "^2.6.1",
    "express": "^4.16.4",
    "react": "^16.6.3",
    "react-dom": "^16.6.3"
  ,
  "devDependencies": 
    "@babel/core": "^7.1.6",
    "@babel/preset-env": "^7.1.6",
    "@babel/preset-react": "^7.0.0",
    "@babel/node": "^7.0.0",
    "babel-jest": "^23.6.0",
    "babel-loader": "^8.0.4",
    "jest": "^23.6.0",
    "nodemon": "^1.18.6",
    "pm2": "^3.2.2",
    "prettier": "1.15.2",
    "regenerator-runtime": "^0.13.1",
    "webpack": "^4.26.1",
    "webpack-cli": "^3.1.2"
  

注意

我正在使用带有 babel 7 的 nodemon。请帮助我。谢谢各位

【问题讨论】:

【参考方案1】:

我在此线程中发现了一个有趣的修复方法,即向您的脚本添加延迟和终止进程命令。

"events": 
  "restart": "fuser -k 5000/tcp ; fuser -k 3050/tcp" // change to whatever ports you're using.

并为 nodemon 命令添加延迟:

 --delay 1500ms

https://github.com/remy/nodemon/issues/1316#issuecomment-387367160

我还注意到,如果我在代码编辑器中快速连续点击两次保存,我会收到此错误。但是,如果我只点击保存一次,我认为没有问题。

【讨论】:

【参考方案2】:

我发现在任何中断时完成正常的服务器关闭可以让 nodemon 成功重新启动(使用 Express):

// Run server
const PORT = process.env.PORT || 3000
const server = app.listen(PORT, () => 
    console.log('[server] Started server');
);

// Graceful shutdown of server
process.on('SIGINT', () => 
    console.log('\n[server] Shutting down...');
    server.close();
    process.exit();
);

process.on('SIGTERM', () => 
    console.log('\n[server] Shutting down...');
    server.close();
    process.exit();
);

process.on('uncaughtException', () => 
    console.log('\n[server] Shutting down...');
    server.close();
    process.exit();
);

【讨论】:

以上是关于错误:使用带有 babel 7 的 nodemon 时监听 EADDRINUSE的主要内容,如果未能解决你的问题,请参考以下文章

nodemon添加babel支持

nodemon,babel-node:如何设置环境变量?

用babel和nodemon搭建一个功能齐全的nodejs开发环境

Babel 7 - 未捕获的 ReferenceError:未定义 regeneratorRuntime

在带有 Babel 的 ReactJS 上使用异步等待会导致错误:意外的令牌

带有 babel 的 webpack 显示错误模块构建失败(来自 ./node_modules/babel-loader/lib/index.js):