vite: Cannot watch specific dependencies in node_modules
Describe the bug
The documentation for server.watch contains the following example:
server: {
watch: {
ignored: ['!**/node_modules/your-package-name/**']
}
},
This example does not work. It appears that the builtin **/node_modules/** exclude causes chokidar to not even look in node_modules despite the negation in the subdirectory.
It appears this was originally tested (see #5023 and #5239) with ignored: ['!**/node_modules/**']. This does work, but in a real project will almost immediately result in Error: ENOSPC: System limit for number of file watchers reached.
See https://github.com/paulmillr/chokidar/issues/1225. I played with various chokidar options but I couldn’t see a way to achieve this.
Reproduction
See chokidar issue.
System Info
System:
OS: Linux 5.4 Linux Mint 20.3 (Una)
CPU: (12) x64 AMD Ryzen 5 2600 Six-Core Processor
Memory: 4.20 GB / 15.56 GB
Container: Yes
Shell: 5.0.17 - /bin/bash
Binaries:
Node: 16.15.1 - /usr/bin/node
npm: 8.1.1 - ~/npm/bin/npm
Browsers:
Chrome: 102.0.5005.61
Firefox: 101.0
npmPackages:
vite: ^2.9.12 => 2.9.12
Used Package Manager
npm
Logs
No response
Validations
- Follow our Code of Conduct
- Read the Contributing Guidelines.
- Read the docs.
- Check that there isn’t already an issue that reports the same bug to avoid creating a duplicate.
- Make sure this is a Vite issue and not a framework-specific issue. For example, if it’s a Vue SFC related bug, it should likely be reported to https://github.com/vuejs/core instead.
- Check that this is a concrete bug. For Q&A open a GitHub Discussion or join our Discord Chat Server.
- The provided reproduction is a minimal reproducible example of the bug.
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 31
- Comments: 24 (4 by maintainers)
Commits related to this issue
- fix: workaround to watch .vitest-preview under node_modules Reference: https://github.com/vitejs/vite/issues/8619#issuecomment-1170762244 — committed to nvh95/vitest-preview by nvh95 2 years ago
- Document working solutions to watch a package inside `node_modules/` The current `server.watch` docs aren't accurate and the solution proposed just doesn't work. See https://github.com/vitejs/vite/is... — committed to MadLittleMods/vite by MadLittleMods a year ago
- docs: update working solutions to watch a package inside `node_modules/` The current `server.watch` docs aren't accurate and the solution proposed just doesn't work. See https://github.com/vitejs/vit... — committed to MadLittleMods/vite by MadLittleMods a year ago
- docs: update working solutions to watch a package inside `node_modules/` The current `server.watch` docs aren't accurate and the solution proposed just doesn't work. See https://github.com/vitejs/vit... — committed to MadLittleMods/vite by MadLittleMods a year ago
Thanks, that’s very helpful as a temporary workaround until chokidar provides an official recommendation of how to fix this properly.
To preempt anyone who tries to close this:
Just packaged up @ryzr’s awesome little snippet into something reusable where you can also list multiple modules if you want 🚀
Then to use it, pass into your
pluginsarray like so:Edit: p.s. Don’t forget to ensure that you exclude these packages from
optimizeDepslike so:Similar to https://github.com/vitejs/vite/issues/6718, it would be nice to exclude locally linked packages from being ignored by default. When I make a change in a sub-dependency, I want the bundle to rebuild.
@SystemParadox
If it’s any help, creating a custom plugin to override the vite-enforced watch options seems to have worked for me
I currently can’t use vite because of this issue. I have a monorepo, where I build dependencies separately. but vite doesn’t detect changes to them and there seems to be no way of making it detect them. The trick with the plugin by @bluwy didn’t work for me either.
The issue with using
optimizeDeps.excludefor this is that this also excludes deps of that package from vite’s esm/cjs interop magic. So you then have to run through and include a bunch of your deps-of-deps inoptimizeDeps.includeto re-opt them in to esm/cjs interop. (@quyle92 that’s probably the issue you’re running into.)I don’t know if something changed in the last month but @patricknelson snippet did not work for me. Been banging my head against this for hours but finally saw this in docs and so tried this:
And it worked! The other “gotcha” I wanted to call out is that if you need to include a dep of your excluded dep, you need to include EXACTLY what is in your import line. For me it was
react-iconsand I wasted a few hours until I realized I had to includereact-icons/fi/index.jsbecause that is what was in the import line in my esm package.@bluwy good thought but alas no, swapping the order doesn’t help:
Chokidar still seems to ignore the whole of
node_modulesand doesn’t bother looking inside it.Ah bummer, I really need it with
vite serve.Was unable to get it working until I added
server.watcher._userIgnored = undefinedpicked from chokidar source (whenconfigureServeris called,server.watcheris already instanciated).This throws error does not provide an export named ‘default’
Hi @TheTedAdams , Thanks for your reply. If I add
optimizeDeps: { exclude: modules, },vite failed to build my app and throw error atnode_modules/uncontrollable/lib/esm/utils.jswith message beingUncaught SyntaxError: ambiguous indirect export: default.