React Router致create-react-app测试失败?如何解决EMFILE报错问题
Hey there! Let me clear this up first: that error you're seeing has nothing to do with React Router directly. It's a system-level issue with file watchers, which just got triggered when your project (with React Router added) hit the limit of how many files your system can monitor at once. Here's how to fix it step by step:
1. Fix System File Descriptor Limits
The EMFILE error means your system has run out of file descriptors (the handles used to track open files). macOS has a relatively low default limit, so let's bump it:
- Temporary fix (current terminal session only):
Run this command in your terminal:
Then restart your test script.ulimit -n 1024 - Permanent fix:
Add the above line to your shell config file (either~/.bash_profile,~/.zshrc, or~/.profiledepending on which shell you use). Save the file, then restart your terminal for changes to take effect.
2. Clean Up Dependencies & Cache
Corrupted node modules or cached files can sometimes exacerbate this issue. Let's do a full reset:
- Delete your
node_modulesfolder:rm -rf node_modules - Clear npm's cache:
npm cache clean --force - Reinstall dependencies:
npm install
3. Update react-scripts to the Latest Version
Older versions of react-scripts had known compatibility issues with FSEvents. Upgrading should resolve most watcher-related bugs:
npm install react-scripts@latest
4. Disable Watch Mode (Temporary Workaround)
If you just need to run tests once without file watching, modify your test script in package.json:
"test": "react-scripts test --env=jsdom --watchAll=false"
This skips the file watching entirely, so you won't hit the descriptor limit.
Why Did It Seem Like React Router Was the Culprit?
Adding React Router probably increased the total number of files in your project (router components, imports, etc.), pushing you over the system's default file watcher limit. The error was a coincidence, not a direct conflict between React Router and CRA's test suite.
内容的提问来源于stack exchange,提问作者iggirex

