簡體   English   中英

當 npm 開始使用 express 和 nodemon 時應用程序崩潰

[英]App crash when npm start using express and 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

所以嘗試一些其他端口,如 3001 或 4000,看看是否有效。

然后你可以找到已經使用 3000 的進程並殺死它,如果它有意義的話。

錯誤:聽 EADDRINUSE:地址已被使用 :::3000

端口3000已在使用中。 另一個程序正在使用該端口(在這種情況下,您必須選擇另一個端口),或者您上次運行該程序時未能正確終止該程序。 如果您在 linux 上運行ps並找到由命令nodemon app.js啟動的進程並復制其pid 然后您通過運行kill后跟您剛剛復制的 pid 來終止該進程。

前任:

$ ps
  PID TTY          TIME CMD
    8 tty1     00:00:00 bash
   32 tty1     00:00:00 nodemon app.js
   33 tty1     00:00:00 ps

$ kill 32

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM