New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot watch specific dependencies in node_modules #8619
Comments
Vite's document seems to be wrong (or there was a behavior change on chokidar side?). |
I also wonder if the order matters: vite/packages/vite/src/node/server/index.ts Lines 284 to 288 in d402ad3
say if we move the user-specified ones at the top before |
@bluwy good thought but alas no, swapping the order doesn't help:
Chokidar still seems to ignore the whole of |
If it's any help, creating a custom plugin to override the vite-enforced watch options seems to have worked for me
|
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:
|
Similar to #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. |
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 workaround above using a custom plugin doesn't work for a plain I tried using the Custom Vite plugin (doesn't work): {
name: 'watch-node-modules',
options(inputOptions) {
inputOptions.watch.chokidar.ignored = [
/node_modules\/(?!hydrogen-view-sdk).*/,
'**/.git/**',
];
return inputOptions;
}
} When using Vite,
|
The current `server.watch` docs aren't accurate and the solution proposed just doesn't work. See vitejs#8619 There is a different workaround in the issue using a custom Vite plugin if you're using the Vite dev server, vitejs#8619 (comment)
The current `server.watch` docs aren't accurate and the solution proposed just doesn't work. See vitejs#8619 There is a different workaround in the issue using a custom Vite plugin if you're using the Vite dev server, vitejs#8619 (comment)
The current `server.watch` docs aren't accurate and the solution proposed just doesn't work. See vitejs#8619 There is a different workaround in the issue using a custom Vite plugin if you're using the Vite dev server, vitejs#8619 (comment)
Just packaged up @ryzr's awesome little snippet into something reusable where you can also list multiple modules if you want 🚀 import { ViteDevServer } from 'vite';
export function pluginWatchNodeModules(modules) {
// Merge module into pipe separated string for RegExp() below.
let pattern = `/node_modules\\/(?!${modules.join('|')}).*/`;
return {
name: 'watch-node-modules',
configureServer: (server: ViteDevServer) : void => {
server.watcher.options = {
...server.watcher.options,
ignored: [
new RegExp(pattern),
'**/.git/**',
]
}
}
}
} Then to use it, pass into your // Import from the separate file you might store this in, e.g. 'utils'
import { pluginWatchNodeModules } from './utils';
plugins: [
// ... other plugins...
pluginWatchNodeModules(['your-plugin', 'another-example']),
], Edit: p.s. Don't forget to ensure that you exclude these packages from optimizeDeps: {
exclude: [
'your-plugin',
'another-example',
],
}, |
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: import type { PluginOption } from 'vite';
export function watchNodeModules(modules: string[]): PluginOption {
return {
name: 'watch-node-modules',
config() {
return {
server: {
watch: {
ignored: modules.map((m) => `!**/node_modules/${m}/**`),
},
},
};
},
};
} 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 |
Many thanks. It works, but I need to |
@quyle92 you may be running into cache stuff. Are you also listing your package in import type { PluginOption } from 'vite';
export function watchNodeModules(modules: string[]): PluginOption {
return {
name: 'watch-node-modules',
config() {
return {
server: {
watch: {
ignored: modules.map((m) => `!**/node_modules/${m}/**`),
},
},
optimizeDeps: {
exclude: modules,
},
};
},
};
} |
Hi @TheTedAdams , |
The issue with using |
I added the plugin in #8619 (comment) and vite updates my linked package when I save the vite config but it still doesn't update when I change the source code. I have |
English is not my native language, and there may be grammar errors in the following content. Please understand. I tried this method, but not work.(antfu/vite-plugin-restart#10)
and use node to change this file // generateId.js
import { readFile, writeFile } from 'fs';
readFile('./.env', 'utf-8', (err, contents) => {
if (err) {
console.error(err);
process.exit(1);
return;
}
contents = `VITE_CHANGE_KEY=${Math.random()}`;
writeFile('./.env', contents, 'utf-8', (err) => {
if (err) {
console.log(err);
} else {
console.log(contents);
}
});
}); Because Vite will observe changes in the. env file and then re-run. // nodemon.json
{
"exec": "npm run *** && npm run generate-id", // generate-id just scripts command => 'node generateId.js'
} It's stupid, but it works. |
@quyle92 you can read package's dependencies and put them to vite
|
This throws error |
Was unable to get it working until I added export function pluginWatchNodeModules (modules) {
return {
name: 'watch-node-modules',
configureServer: (server) => {
const regexp = `/node_modules\\/(?!${modules.join('|')}).*/`
server.watcher.options = {
...server.watcher.options,
ignored: [
'**/.git/**',
'**/test-results/**',
new RegExp(regexp)
]
}
server.watcher._userIgnored = undefined
},
config () {
return {
optimizeDeps: {
exclude: modules
}
}
}
}
} |
Mb anyone worked with nuxt 3
|
Did work for me: function watchPackages(packageNames) {
let isWatching = false;
return {
name: 'vite-plugin-watch-packages',
buildStart() {
if (!isWatching) {
packageNames.forEach((packageName) => {
const absPackagePath = path.resolve('node_modules', packageName);
const realPackagePath = fs.realpathSync(absPackagePath);
this.addWatchFile(realPackagePath);
});
isWatching = true;
}
},
};
}
// in vite.config.js
{
plugins: [watchPackages(['dayjs', 'foo/bar', '@some-scoped-package/utils'])]
} But it works only for |
Thanks for sharing @acupofspirt! This still doesn't seem to work for me in https://github.com/vega/editor if I link https://github.com/vega/vega-lite. I run |
@domoritz It will work only for |
Ah bummer, I really need it with |
If it helps, it works with in the config: |
Hi @acupofspirt, I tried using this plugin. It's great and worked well for the files inside the src or the root but not in the module graph. But it did not work for the files inside the node_modules. I did use the May I know your vite version? Thanks! |
Any file of other projects inside the node_modules can't be watched expectedly either. It's weird. |
Describe the bug
The documentation for server.watch contains the following example:
This example does not work. It appears that the builtin
**/node_modules/**
exclude causes chokidar to not even look innode_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 inError: ENOSPC: System limit for number of file watchers reached
.See paulmillr/chokidar#1225. I played with various chokidar options but I couldn't see a way to achieve this.
Reproduction
See chokidar issue.
System Info
Used Package Manager
npm
Logs
No response
Validations
The text was updated successfully, but these errors were encountered: