执行npm run start报错EMFILE: too many open files,已尝试多方案未解决
问题
执行npm update后项目启动失败,运行npm run start时返回如下错误:
10% building 0/1 entries 0/0 dependencies 0/0 modulesnode:internal/errors:484 ErrorCaptureStackTrace(err); ^ Error: EMFILE: too many open files, watch at FSWatcher._handle.onchange (node:internal/fs/watchers:204:21) Emitted 'error' event on FSWatcher instance at: at FSWatcher._handleError (/Users/199/WebstormProjects/isu_frontend_common/node_modules/chokidar/index.js:647:10) at NodeFsHandler._boundHandleError (/Users/199/WebstormProjects/isu_frontend_common/node_modules/chokidar/lib/nodefs-handler.js:303:43) at /Users/199/WebstormProjects/isu_frontend_common/node_modules/chokidar/lib/nodefs-handler.js:137:5 at foreach (/Users/199/WebstormProjects/isu_frontend_common/node_modules/chokidar/lib/nodefs-handler.js:41:5) at fsWatchBroadcast (/Users/199/WebstormProjects/isu_frontend_common/node_modules/chokidar/lib/nodefs-handler.js:136:3) at FSWatcher.<anonymous> (/Users/199/WebstormProjects/isu_frontend_common/node_modules/chokidar/lib/nodefs-handler.js:185:9) at FSWatcher.emit (node:events:513:28) at FSWatcher._handle.onchange (node:internal/fs/watchers:210:12) { errno: -24, syscall: 'watch', code: 'EMFILE', filename: null } Node.js v18.12.1 node:internal/process/promises:288 triggerUncaughtException(err, true /* fromPromise */); ^ RpcIpcMessagePortClosedError: Cannot send the message - the message port has been closed for the process 17332.
已尝试以下方案但未解决:
- 删除
node_modules和package-lock.json,清理缓存后执行npm install - 切换Node.js和NPM至最新稳定版本
- 在其他文件夹重新安装项目
可行解决办法
1. 提升系统文件描述符限制(macOS/Linux)
EMFILE错误本质是系统允许打开的文件数上限不足,可临时或永久提升限制:
- 临时生效(当前终端会话):
执行完后重新运行ulimit -n 10240npm run start - 永久生效(macOS):
创建或编辑~/Library/LaunchAgents/limit.maxfiles.plist文件,写入以下内容:
保存后执行<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> <key>Label</key> <string>limit.maxfiles</string> <key>ProgramArguments</key> <array> <string>launchctl</string> <string>limit</string> <string>maxfiles</string> <string>10240</string> <string>10240</string> </array> <key>RunAtLoad</key> <true/> <key>ServiceIPC</key> <false/> </dict> </plist>launchctl load ~/Library/LaunchAgents/limit.maxfiles.plist,重启终端生效。
2. 调整Chokidar监听配置
项目使用的Chokidar(文件监听库)可能监听了过多文件,可在项目配置中忽略不必要的目录:
- 若为Webpack项目,修改
webpack.config.js的watchOptions:module.exports = { // ...其他配置 watchOptions: { ignored: /node_modules|\.git/ // 忽略node_modules和.git目录 } }; - 若为Vue项目,在
vue.config.js中添加:module.exports = { configureWebpack: { watchOptions: { ignored: /node_modules/ } } };
3. 安装fsevents优化监听(macOS)
安装Chokidar的fsevents依赖,提升监听效率并减少文件描述符占用:
npm install fsevents --save-dev
4. 关闭冗余后台进程
检查系统中打开的进程,关闭不需要的应用或服务,释放文件描述符资源:
- macOS:通过「活动监视器」查看并关闭多余进程
- Linux:执行
lsof | wc -l查看当前打开文件数,用kill命令终止不必要的进程
5. 回退到更新前的依赖版本
若为npm update后特定依赖导致的问题,可利用Git回退依赖状态:
git checkout HEAD~1 package-lock.json rm -rf node_modules npm install
内容的提问来源于stack exchange,提问作者8Irbis8
相关产品推荐
相关产品推荐

