首次运行React测试遇FSEvents及EMFILE错误求助
Hey there, I see you're hitting some frustrating errors when trying to run your React tests. Let's break down what's happening and how to fix it.
First, let's recap the error you're seeing (formatted for clarity):
2018-05-30 16:47 node[3217] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22) 2018-05-30 16:47 node[3217] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22) 2018-05-30 16:47 node[3217] (FSEvents.framework) FSEventStreamStart: register_with_server: ERROR: f2d_register_rpc() => (null) (-22) events.js:183 throw er; // Unhandled 'error' event ^ Error: Error watching file for changes: EMFILE at _errnoException (util.js:1022:11) at FSEvent.FSWatcher._handle.onchange (fs.js:1359:9) npm ERR! code ELIFECYCLE npm ERR! errno 1 npm ERR! isomorphic@2.6.0 test: `react-app-rewired test --env=jsdom` npm ERR! Exit status 1 npm ERR! npm ERR! Failed at the isomorphic@2.6.0 test script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! /Users/EcoSurvUser/.npm/_logs/2018-05-30T15_47_27_998Z-debug.log Alessandros-MacBook-Pro-2:isomorphic_immutable_js EcoSurvUser$
What's Causing This?
The two main culprits here are:
- EMFILE Error: This means your system has hit the limit for open file descriptors. React tests (and tools like
react-app-rewired) watch a lot of files, so it's easy to hit this limit on macOS. - FSEvents Framework Errors: FSEvents is macOS's file watching system, and these errors are a side effect of the file descriptor limit or issues with
watchman(the tool React uses for file watching).
Step-by-Step Fixes
1. Temporarily Increase File Descriptor Limit
First, let's raise the number of allowed open files for your current terminal session:
ulimit -n 10240
Now try running your tests again with npm test. If this works, you'll want to make this change permanent so you don't have to run it every time:
- If you use Bash, add the line
ulimit -n 10240to~/.bash_profile - If you use Zsh, add it to
~/.zshrc
Then runsource ~/.bash_profile(orsource ~/.zshrc) to apply the change immediately.
2. Fix Watchman Issues
Watchman is often the root cause of FSEvents errors. Try these steps:
- Restart Watchman:
Then run your tests again.watchman shutdown-server watchman watch-del-all - Reinstall Watchman (if restarting doesn't work):
(If you don't have Homebrew installed, you can skip this step or install it first.)brew uninstall watchman brew install watchman
3. Clean Up Node Modules & Cache
Sometimes corrupted dependencies can cause file watching issues. Let's do a full reset:
rm -rf node_modules npm cache clean --force npm install
Once reinstalled, try running your tests again.
Final Note
If none of these steps work, you can also try running the tests without file watching by adding the --watchAll=false flag:
npm test -- --watchAll=false
This will run the tests once instead of watching for changes, which avoids the file watching issues entirely.
内容的提问来源于stack exchange,提问作者Istvan Orban

