Skip to content

Webpack 5 watch mode does not trigger compilation when node_modules file changes #11612

Closed

Description

@jasongrout

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 #11406 as requested there.

Activity

  1. sokra commented on Oct 8, 2020

    @sokra
    Member

    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.

  2. jasongrout commented on Oct 8, 2020

    @jasongrout
    ContributorAuthor

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

  3. alexander-akait commented on Oct 8, 2020

    @alexander-akait
    Member

    A recommendation to avoid using it

  4. jasongrout commented on Oct 8, 2020

    @jasongrout
    ContributorAuthor

    A recommendation to avoid using it

    Noted, thanks.

  5. Akryum commented on May 4, 2021

    @Akryum
    Contributor

    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?

  6. alexander-akait commented on May 5, 2021

    @alexander-akait
    Member

    it works fine with monorepos, how you use monorepos, can you provide more information?

  7. Akryum commented on May 5, 2021

    @Akryum
    Contributor

    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.

  8. alexander-akait commented on May 5, 2021

    @alexander-akait
    Member

    Why do you disable symlink?

  9. Akryum commented on May 5, 2021

    @Akryum
    Contributor

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

  10. alexander-akait commented on May 5, 2021

    @alexander-akait
    Member

    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

  11. guico33 commented on May 17, 2021

    @guico33

    Facing the same issue with a Lerna monorepo.
    Even with

        snapshot: {
          managedPaths: [],
          immutablePaths: [],
        },
        symlinks: true,
    

    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.managedPaths to [] fixes the issue in a Lerna monorepo.

  12. slorber commented on Jul 13, 2021

    @slorber

    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)

    I'm trying the workaround:

        cache: false,
        snapshot: {
          managedPaths: [],
          immutablePaths: [],
        },

    That does not work for me unfortunately :'(


    Edit: actually figured this out:

    • need to use watchOptions.followSymlinks = true (even if resolve.symlinks = false)
    • need to refactor recommended optimization watchOption.ignored = /node_modules/

    So the cache bug is the only one remaining

  13. alexander-akait commented on Jul 13, 2021

    @alexander-akait
    Member

    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?

  14. alexander-akait commented on Jul 13, 2021

    @alexander-akait
    Member

    Ideally you should not disable symlinks, if you have problems with this, better to fix it, disable it should be workaround for cache only

  15. slorber commented on Jul 13, 2021

    @slorber

    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.

  16. alexander-akait commented on Jul 13, 2021

    @alexander-akait
    Member

    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.

    I mean change something in node_modules, except linked repos, i.e. workspaces

  17. gaurav5430 commented on Feb 28, 2023

    @gaurav5430

    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,
    },
    
  18. rsodre commented on Jun 30, 2023

    @rsodre

    @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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions