javascript - 当 npm 开始使用 express 和 nodemon 时应用程序崩溃
问题描述
我有一个简单的app.js
-
const express = require('express');
const app = express();
//Route
app.get('/', (req, res) => {
res.send('Welcome to custom api');
})
//Listen
app.listen(3000);
我的 package.json 看起来像这样 -
{
"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"
}
}
我运行npm start
并收到此错误 -
> 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...
解决方案
以下错误意味着3000已在使用中,可能是您从另一个终端运行它,或者其他应用程序正在使用它。
错误:监听 EADDRINUSE:地址已在使用 :::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.
推荐阅读
- javascript - 待办事项列表删除按钮无法正常工作
- python - Python 调用带参数的 shell 脚本
- javascript - v-for inside v-for 在 onclick 事件中显示数据
- python - 遍历字典和 TypeError:'str' 和 'int' 的实例之间不支持'>=' - Python
- django - 我对Django的用户认证系统有一点误解
- android - 下载文件时未显示下载管理器通知
- c++ - 如何在 VS Code 中导入 QT c++ 库
- html - 一个元素是否可以同时具有自定义形状和盒子阴影?
- serialization - 如何取回悬挂的物体
- unity3d - 更新到统一后 2021.1.13.f1 isNetworkError 和 isHttpError 已经过时