You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Node.js启动报错listen EACCES: permission denied 排查与解决

启动Node.js服务器时出现listen EACCES权限拒绝错误,更换端口仍无法解决

执行npm run start-dev启动Node.js服务器时,突然抛出listen EACCES: permission denied错误,更换端口后问题依旧。以下是具体报错信息、配置文件及代码:

报错信息

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error: listen EACCES: permission denied 30000;
    at Server.setupListenHandle [as _listen2] (node:net:1468:21)
    at listenInCluster (node:net:1533:12)
    at Server.listen (node:net:1632:5)
    at Function.listen (D:\ecommerce-api\node_modules\express\lib\application.js:635:24)
    at StartDB (D:\ecommerce-api\app.js:34:9)
    at Object.<anonymous> (D:\ecommerce-api\app.js:42:1)
    at Module._compile (node:internal/modules/cjs/loader:1159:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
    at Module.load (node:internal/modules/cjs/loader:1037:32)
    at Module._load (node:internal/modules/cjs/loader:878:12)
Emitted 'error' event on Server instance at:
    at emitErrorNT (node:net:1512:8)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'EACCES',
  errno: -4092,
  syscall: 'listen',
  address: '5000;',
  port: -1
}

Node.js v18.12.1
[nodemon] app crashed - waiting for file changes before starting...

.env配置文件

PORT=5000;
MONGO_URI=mongodb://127.0.0.1:27017/ecommerce

app.js代码

require("dotenv").config();
const express = require("express");
const app = express();
const port = process.env.PORT || 5000;

//Database
const connectDB = require("./DB/ConnectDB");
//Paths
const authRoute = require("./Routes/AuthRoute");

//Middleware
app.use(express.json());

app.get("/", (req, res) => {
  res.send("Ecommerce API!");
});

app.use("/api/v1/auth", authRoute);

const StartDB = async () => {
  try {
    connectDB(process.env.MONGO_URI);
    app.listen(port, () => {
      console.log(`App listening on port ${port}`);
    });
  } catch (error) {
    console.log(error);
  }
};

StartDB();

package.json文件

{
  "name": "ecommerce-api",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.js",
    "start-dev": "nodemon app.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "bcryptjs": "^2.4.3",
    "cors": "^2.8.5",
    "dotenv": "^16.0.3",
    "express": "^4.18.2",
    "express-async-errors": "^3.1.1",
    "http-status-codes": "^2.2.0",
    "jsonwebtoken": "^9.0.0",
    "mongoose": "^6.8.4",
    "morgan": "^1.10.0",
    "validator": "^13.7.0"
  }
}

问题原因及解决步骤

核心问题

你的.env文件中PORT=5000;末尾多了一个分号;,导致process.env.PORT读取到的是字符串"5000;"而非数字5000。当app.listen()接收到这个带分号的字符串时,会错误地将其解析为要监听的IP地址,而非端口号,进而触发权限拒绝错误(因为你没有权限绑定到这个无效的"地址"上)。

解决步骤

  1. 修改.env文件,删除PORT值末尾的分号:
PORT=5000
MONGO_URI=mongodb://127.0.0.1:27017/ecommerce
  1. 重启nodemon服务(如果没有自动重启,手动执行npm run start-dev)。

额外优化建议

为了避免类似字符串转数字的问题,可以在读取端口时显式转换为数字:

const port = parseInt(process.env.PORT, 10) || 5000;

这样即使.env中出现格式问题,也能确保port是有效的数字类型。


内容的提问来源于stack exchange,提问作者Nannu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.03 09:46:05