You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Webpack 5 watch mode does not trigger compilation when node_modules file changes #11612
Webpack 5 RC 4 in watch mode does not respond to a change in a file in node_modules. Webpack 4 does.
If the current behavior is a bug, please provide the steps to reproduce.
cd /tmp
mkdir webpack5-nm-check
cd webpack5-nm-check
npm init -y
npm install webpack@~5.0.0-rc.4 webpack-cli isarray
mkdir src
echo"require('isarray')"> src/index.js
./node_modules/.bin/webpack --mode development --watch
In a separate terminal, run:
cd /tmp/webpack5-nm-check
echo"console.log('hello');"> ./node_modules/isarray/index.js
Note that the webpack compilation is not triggered on this change.
To show this works in webpack 4:
cd /tmp
mkdir webpack4-nm-check
cd webpack4-nm-check
npm init -y
npm install webpack webpack-cli isarray
mkdir src
echo"require('isarray')"> src/index.js
./node_modules/.bin/webpack --mode development --watch
and
cd /tmp/webpack4-nm-check
echo"console.log('hello');"> ./node_modules/isarray/index.js
Note that the webpack 4 build re-compiles.
What is the expected behavior?
Webpack 5 should recompile when an imported module in node_modules changes. This can happen, for example, in a monorepo where the node_modules module actually links to the source of another package.
By default, webpack assumes that the node_modules directory, which webpack is inside of, is only modified by a package manager. Hashing and timestamping is skipped for node_modules. Instead, only the package name and version is used for performance reasons. Symlinks (i. e. npm/yarn link) are fine. Do not edit files in node_modules directly unless you opt-out of this optimization with snapshot.managedPaths: []. When using Yarn PnP webpack assumes that the yarn cache is immutable (which it usually is). You can opt-out of this optimization with snapshot.immutablePaths: []
This can happen, for example, in a monorepo where the node_modules module actually links to the source of another package.
In this case this should work as expected.
Reacted by Dmitry Starosta, IBICF, leusrox, David Boho, Julio Gonzalez, Tor Nordqvist, Dariusz Filipiak, Thinking80s, sshetko and Andrii Oriekhov
Thanks for the explanation and nuanced answer. I confirmed that snapshot.managedPaths: [] does indeed solve the problem in the reproduction above. Closing as answered.
This change is breaking mono-repos with workspaces in dependencies like vue-devtools which is pretty common.
I need to put snapshot.managedPaths: [] otherwise changes in app-frontend for example are ignored with webpack serve. So by default the new configuration breaks those projects. Maybe it should be reverted?
Usually I have problems with them with (for example PostCSS config not found errors)
Should be no problems with this, maybe you can update example above and show the problem? if you disable symlink, symlink will not work, even we revert this behavior
Also affected by this with Docusaurus, since turning symlinks from true to false I see a lot of problems in our monorepo:
dev only hot reload for changes in some parts the codebase (packages using Yarn workspace symlinks)
Netlify prod/deploy previews deploy stale code due to persistent caching.
Change PR, than can serve as a repro: meta/docusaurus#5126 + yarn install && yarn start:v2 + yarn watch
We want to turn symlinks: false because we are a doc tool and our users want to use symlinks for the root folder of their docs (containing markdown file), as this folder can come from another directly.
We use MDX and our mdx-loader should be able to handle all the content of that symlinked directly (but nothing more, because we use multiple mdx loaders).
As far as I understand:
There is a bug related to the cache, that I can confirm, affecting both dev/prod builds, and may be solved by PR fix: fix FileSystemInfo#createSnapshot with symbol link #12254 . Disabling the cache works but is not manageable, particularly slowing down the dev experience.
There is a bug related to the dev server not recompiling when a node_modules/monorepo-linked-package is updating, requiring to run yarn start again to see the changes (or find another way to trigger the recompilation, which is very slow because the dev cache had to be disabled)
There is a bug related to the cache, that I can confirm, affecting both dev/prod builds, and may be solved by PR fix: fix FileSystemInfo#createSnapshot with symbol link #12254 . Disabling the cache works but is not manageable, particularly slowing down the dev experience.
Yep, here bug, maybe we need prioritize it
There is a bug related to the dev server not recompiling when a node_modules/monorepo-package is updating, requiring to run yarn start again to see the changes (or find another way to trigger the recompilation, which is very slow because the dev cache had to be disabled)
Why you need this? It is fully unsafe, it will not work with yarn PnP, pnpm, etc?
Why you need this? It is fully unsafe, it will not work with yarn PnP, pnpm, etc?
I don't understand what you mean here, this is the default mode of a Yarn workspace and all monorepos I work on behave this way.
The Docusaurus website consumes its own monorepo packages , exposed as symlinks
Symlink ./node_modules/@docusaurus/monorepo-package targets ./packages/monorepo-package
And we do update ./packages/monorepo-package/dist/MyReactComponent.js in dev and expect hot reload to work (like it used to do)
Ideally you should not disable symlinks, if you have problems with this, better to fix it, disable it should be workaround for cache only
When enabled, symlinked resources are resolved to their real path, not their symlinked location. Note that this may cause module resolution to fail when using tools that symlink packages (like npm link).
I am using tools that symlink packages, and our users may too 🤪
Bugs have even been reported by our users when trying to use doc symlinks and users applying symlinks: false themselves in userland is a workaround that has been applied for some time now.
So shouldn't I expect it to work better with symlinks: false actually?
Ideally, I'd want Webpack to only resolve some symlinks, not all.
We use symlinks: false because we want the dependencies of the symlinked modules to be resolved from the parent module.
With the above suggestions, overriding managedPaths to NOT ignore node_modules for hashing did not work for us alone.
What worked though was adding watchOptions: { followSymlinks: true }
Somehow it seems that with symlinks: false, by default the changes in the symlinked files are not watched anymore by the dev server ( I don't remember this happening in webpack 4, everything worked fine out of the box)
Anyway, this is the final config that works for us:
snapshot: {
managedPaths: [],
},
// when symlinks.resolve is false, we need this to make sure dev server picks up the changes in the symlinked files and rebuilds
watchOptions: {
followSymlinks: true,
},
resolve: {
// Uncomment the following line when working with local packages
// More reading : https://webpack.js.org/configuration/resolve/#resolvesymlinks
symlinks: false,
},
Reacted by Praveen Puglia, Stephan Tang, Pich Him, Oleg Gun, Ivan Voskoboinyk, Andrii Oriekhov, roger_s, Théis Bazin, Alec Mev, Moinul Moin and 5 more
Activity
sokra commentedon Oct 8, 2020
By default, webpack assumes that the
node_modulesdirectory, which webpack is inside of, is only modified by a package manager. Hashing and timestamping is skipped fornode_modules. Instead, only the package name and version is used for performance reasons. Symlinks (i. e. npm/yarn link) are fine. Do not edit files in node_modules directly unless you opt-out of this optimization withsnapshot.managedPaths: []. When using Yarn PnP webpack assumes that the yarn cache is immutable (which it usually is). You can opt-out of this optimization withsnapshot.immutablePaths: []In this case this should work as expected.
jasongrout commentedon Oct 8, 2020
Thanks for the explanation and nuanced answer. I confirmed that
snapshot.managedPaths: []does indeed solve the problem in the reproduction above. Closing as answered.alexander-akait commentedon Oct 8, 2020
A recommendation to avoid using it
jasongrout commentedon Oct 8, 2020
Noted, thanks.
Akryum commentedon May 4, 2021
This change is breaking mono-repos with workspaces in
dependencieslike vue-devtools which is pretty common.I need to put
snapshot.managedPaths: []otherwise changes in app-frontend for example are ignored withwebpack serve. So by default the new configuration breaks those projects. Maybe it should be reverted?fix: hmr not working, see webpack/webpack#11612 (comment)
alexander-akait commentedon May 5, 2021
it works fine with monorepos, how you use monorepos, can you provide more information?
Akryum commentedon May 5, 2021
Here is a reproduction: https://github.com/Akryum/webpack-issue-11612
yarnpackages/frontyarn devpackages/components/src/Components.jsfileExpected: hot reload
Actual:

It seems that using
symlink: falsecauses this.alexander-akait commentedon May 5, 2021
Why do you disable
symlink?Akryum commentedon May 5, 2021
Usually I have problems with them with (for example PostCSS config not found errors)
alexander-akait commentedon May 5, 2021
Should be no problems with this, maybe you can update example above and show the problem? if you disable
symlink,symlinkwill not work, even we revert this behaviorguico33 commentedon May 17, 2021
Facing the same issue with a Lerna monorepo.
Even with
the problem persists, app is not rebuilt after making changes to a package's code.
EDIT: I mixed up dev and prod config when doing my testing. I confirm setting
snapshot.managedPathsto[]fixes the issue in a Lerna monorepo.fix: hmr not working, see webpack/webpack#11612 (comment)
slorber commentedon Jul 13, 2021
Also affected by this with Docusaurus, since turning
symlinksfrom true to false I see a lot of problems in our monorepo:Change PR, than can serve as a repro: meta/docusaurus#5126 +
yarn install && yarn start:v2+yarn watchWe want to turn
symlinks: falsebecause we are a doc tool and our users want to use symlinks for the root folder of their docs (containing markdown file), as this folder can come from another directly.We use MDX and our mdx-loader should be able to handle all the content of that symlinked directly (but nothing more, because we use multiple mdx loaders).
As far as I understand:
node_modules/monorepo-linked-packageis updating, requiring to runyarn startagain to see the changes (or find another way to trigger the recompilation, which is very slow because the dev cache had to be disabled)I'm trying the workaround:
That does not work for me unfortunately :'(
Edit: actually figured this out:
watchOptions.followSymlinks = true(even ifresolve.symlinks = false)watchOption.ignored = /node_modules/So the cache bug is the only one remaining
alexander-akait commentedon Jul 13, 2021
Yep, here bug, maybe we need prioritize it
Why you need this? It is fully unsafe, it will not work with yarn PnP, pnpm, etc?
alexander-akait commentedon Jul 13, 2021
Ideally you should not disable
symlinks, if you have problems with this, better to fix it, disable it should be workaround for cache onlyslorber commentedon Jul 13, 2021
I don't understand what you mean here, this is the default mode of a Yarn workspace and all monorepos I work on behave this way.
The Docusaurus website consumes its own monorepo packages , exposed as symlinks
Symlink
./node_modules/@docusaurus/monorepo-packagetargets./packages/monorepo-packageAnd we do update
./packages/monorepo-package/dist/MyReactComponent.jsin dev and expect hot reload to work (like it used to do)I'm not sure to understand that. Reading the doc, I feel using
symlinks: falseis the safest choice: https://webpack.js.org/configuration/resolve/#resolvesymlinksI am using tools that symlink packages, and our users may too 🤪
Bugs have even been reported by our users when trying to use doc symlinks and users applying
symlinks: falsethemselves in userland is a workaround that has been applied for some time now.So shouldn't I expect it to work better with
symlinks: falseactually?Ideally, I'd want Webpack to only resolve some symlinks, not all.
alexander-akait commentedon Jul 13, 2021
I mean change something in
node_modules, except linked repos, i.e. workspacesgaurav5430 commentedon Feb 28, 2023
We use
symlinks: falsebecause we want the dependencies of the symlinked modules to be resolved from the parent module.With the above suggestions, overriding
managedPathsto NOT ignore node_modules for hashing did not work for us alone.What worked though was adding
watchOptions: { followSymlinks: true }Somehow it seems that with symlinks: false, by default the changes in the symlinked files are not watched anymore by the dev server ( I don't remember this happening in webpack 4, everything worked fine out of the box)
Anyway, this is the final config that works for us:
build: make the watch rebuild faster again
rsodre commentedon Jun 30, 2023
@gaurav5430 finally, a solution for this, outdated linked modules were driving me crazy!
this is working with linked common js modules but not for typescript. any ideas why?