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

NodeJS应用在5000端口启动报app crashed错误的原因排查

问题解答

核心原因

M1/M2系列Mac上,5000端口默认被系统的AirPlay Receiver服务占用,这就是你在该端口启动Node服务失败的直接原因。

验证端口占用

你可以通过终端命令确认这一点:

lsof -i :5000

执行后会看到占用端口的进程为ControlCenter或AirPlayXPCHelper,属于系统内置服务。

解决办法

方法1:临时释放端口

打开「系统设置」→「通用」→「隔空投送与接力」,关闭「允许AirPlay接收」选项,5000端口会立即释放,此时你可以正常在该端口启动服务。

方法2:永久规避(保留AirPlay功能)

如果不想关闭AirPlay服务,直接改用其他端口(比如你已经尝试的4999)是最简单的方案;或者通过终端命令修改AirPlay的监听端口(需要重启系统):

sudo defaults write /Library/Preferences/com.apple.airplay.plist port -integer 5001

代码优化建议(针对TypeScript新手)

你的代码可以调整为更标准的ES模块写法,避免混用CommonJS和ES模块:

  • app.ts 修改导出方式:
import express, { Application, Request, Response, NextFunction } from 'express';
import cookieParser from 'cookie-parser'; // 改用ES模块导入

const app: Application = express();

app.use(express.json());
app.use(cookieParser());

app.get('/', (req: Request, res: Response, next: NextFunction) => {
    res.status(200).json({ success: 'Hello Server'});
});

export default app; // 改用ES模块导出
  • server.ts 修改导入方式:
import http from 'http';
import app from './app'; // 改用ES模块导入

const port = process.env.PORT || 5000;
const server = http.createServer(app);

server.listen(port, () => console.log(`Running on port ${port}`));

同时确保你的package.json中添加"type": "module",或者在tsconfig.json中设置"module": "ESNext"并配置相应的编译选项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 17:33:15