首页 > 解决方案 > 当 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...

标签: javascriptnode.jsexpress

解决方案


以下错误意味着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.


推荐阅读