Monday, 22 June 2020

Error: listen EADDRINUSE: address already in use :::3001 [Nodemon]

I used a Windows 10 laptop for a long time and never had this error. Now I just bought my first MacBook and I sometimes I get this error with my Node.js / React application.I use Nodemon so when I save then I don't have to manually restart the server etc. The first time it usually works, then when I hit ctrl+c to kill the server or restart VS Code, and run npm run dev again then this error appears. Sometimes just every second time after saving changes.Error: listen EADDRINUSE: address already in use :::3001And most of the times when I hit ctrl+c to stop the server then I get a Nodemon error status 130.What's going on?index.js:const express = require("express");const path = require("path");const cors = require("cors");const bodyParser = require("body-parser");const http = require("http");const app = express();const server = http.createServer(app);app.use(express.json({ extended: false, limit: "5mb" }));app.use(cors());app.use(bodyParser.urlencoded({ extended: false }));app.use(bodyParser.json());if (process.env.NODE_ENV === "production") {app.use(express.static("client/build"));app.get("*", (req, res) =>res.sendFile(path.resolve(__dirname, "client", "build", "index.html")));}const PORT = process.env.PORT || 3001;server.listen(PORT, () => console.log(\Server started on port ${PORT}`));`​Package.json for backend:{"name": "questions","version": "1.0.0","description": "","main": "index.js","scripts": {"start": "node index.js","server": "nodemon index.js","client": "npm start --prefix client","dev": "concurrently \"npm run server\" \"npm run client\""},"author": "","license": "ISC","dependencies": {"bcryptjs": "^2.4.3","body-parser": "^1.19.0","concurrently": "^5.2.0","config": "^3.3.1","cors": "^2.8.5","express": "^4.17.1","jsonwebtoken": "^8.5.1","mongo": "^0.1.0","mongodb": "^3.5.9","mongoose": "^5.9.19","node-sass": "^4.14.1","nodemon": "^2.0.4","uuid": "^8.1.0"}}​On the client side I just have a freshly initialized React app (npx create-react-app).

Submitted June 22, 2020 at 12:07PM by alternativeWildBear

No comments:

Post a Comment