-
-
Notifications
You must be signed in to change notification settings - Fork 6.3k
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. |
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. |
Hi, is there anyone still care about it? ALL workarounds above not work for me, that's quite irritating... I am spending the whole night for this thing that shouldn't happen at all. |
✋ me for sure. It's been bugging my team for a while. |
I dont use command "vite", which does not reflect changes in node_modules correctly. Instead I use these scripts:
This checks for all changes (even in node_modules) and restarts the dev server. It is triggered by changes in my linked package without problem. |
Oh nice. How fast is that? I switched to parcel which seems to work with watching deps if I set |
Hmm, I tried your suggestion @maylow22 but vite doesn't seem to rebuild when I change the sources in my linked dependency. Do you have an example repo I can try or a specific config/plugin? |
It does not listen for changes in your source code, but rather in the built package. Therefore, I have a watcher script in a linked package that rebuilds the linked package (shared library) whenever a change in the source code occurs:
After this watcher finishes its build, another watcher in the main app triggers a build in the main app. I don't claim it's a perfect solution, but it works. It is slower than restarting a development server, and since it performs a production build, it might not be ideal. |
Tried to solve the same problem for gui. Currently you have to re-save the vite.config.ts file in order for dev server to rebuild with changed dependencies. Had to add force flag and optimizeDeps.force: true to vite.config.ts to make this work. This problem is bugging a lot of vite users: vitejs/vite#8619 . Also refactored clientToNodeTransformer to not depend on z.instanceof, because it is not reliable...
Waiting for a best practice.🤖 |
Chokidar v4 is going to drop support for globs altogether (see paulmillr/chokidar#1225). So this is now firmly in vite's arena. Vite now has the opportunity to show how this can be done right and hopefully other tools will follow vite's example. As far as I can work out, for any kind of include/exclude system to avoid this problem you need 3 options:
This works pretty much as it does now, Different naming or other combinations of 3 options may be possible (I considered Obviously there are more complicated options which would also work, such as an ordered list of entries with |
The workaround with patching watcher options in configureServer can be improved a little by replacing the existing node_modules ignore, so that other custom ignores are preserved: function pluginWatchNodeModules(modules) {
let pattern = `/node_modules\\/(?!${modules.join('|')}).*/`;
return {
name: 'watch-node-modules',
configureServer: (server: ViteDevServer) : void => {
const node_modules_i = server.watcher.options.ignored.indexOf('**/node_modules/**');
server.watcher.options.ignored.splice(node_modules_i,1,new RegExp(pattern));
server.watcher._userIgnored=undefined;
},
}
} This does give you file change events for files in the modules listed and triggers vite hmr, however plugins handleHotUpdate hook can get called with an incomplete context if you are using pnpm due to a mismatch between the symlink and real path of the module inside node_modules. For that to work, vite needs to update vite/packages/vite/src/node/server/hmr.ts Line 120 in b55c32f
file before finding affected modules.
|
If you are creating with UI in vite, maybe you want Then you will frustrated to find that the page doesn't find the latest lib! But you can try it as follows:
It's work on my case |
MAC is successful, Windows is invalid |
Tried everything on this page and could not get a dependency (workspace: or portal: in Yarn) to correctly HMR, or even refresh the cache to take changes in the dependency. Going to have to move away from the dev server to a much worse dummy Express server and do a Has anyone got this working with a scoped package ( |
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: