webpack: Webpack 5 watch mode does not trigger compilation when node_modules file changes

Bug report

What is the current behavior?

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.

Other relevant information: webpack version: 5.0.0-rc.4 Node.js version: 14.13.0 Operating System: macOS 10.14.6 Additional tools:

Referencing https://github.com/webpack/webpack/issues/11406 as requested there.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 3
  • Comments: 18 (13 by maintainers)

Commits related to this issue

Most upvoted comments

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,
},

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.

Here is a reproduction: https://github.com/Akryum/webpack-issue-11612

  1. Run yarn
  2. Go to packages/front
  3. Run yarn dev
  4. Open the browser console to see the message
  5. Change the packages/components/src/Components.js file

Expected: hot reload

Actual: image

It seems that using symlink: false causes this.

A recommendation to avoid using it

Thanks for the explanation and nuanced answer. I confirmed that snapshot.managedPaths: [] does indeed solve the problem in the reproduction above. Closing as answered.

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

I’m not sure to understand that. Reading the doc, I feel using symlinks: false is the safest choice: https://webpack.js.org/configuration/resolve/#resolvesymlinks

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.

Usually I have problems with them with (for example PostCSS config not found errors)