next.js: Hot reloading of linked modules doesn't work on server

Bug report

When working with next and a linked module, the hot reloading doesn’t work on the server but only on the client.

Describe the bug

When you have a linked module inside the node_modules folder the module is only replaced on the client but not on the server. This means that you have to restart the whole next application in order to render the updated module on the server. When the module is a React.Component and the output change this even results in inconsistent rendering between server and client shown in a error message in the console.

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Setup clean next-js app
  2. Link a module via npm link my-module
  3. Import the module anywhere e.g. pages/index
  4. Start next-js in dev mode
  5. Change your linked module e.g. add console.log or if React.Component change render method. this will trigger a reload (see terminal)
  6. Changes are only visible in the client. server still loads the previous version of the module (probably from cache)

Expected behavior

The require.cache gets cleared properly so the linked module will be re-required also on the server. so client and server stay in sync.

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 16 (15 by maintainers)

Most upvoted comments

@cherniavskii I made a webpack plugin to temporary solve this problem in the projects i am working on: https://www.npmjs.com/package/webpack-clear-require-cache-plugin

Its probably not the cleanest solution but it gets the job done

@timneutkens Thanks for looking into this! Indeed, it looks similar. Meanwhile, I’ve created minimal reproduction example here: https://github.com/cherniavskii/nextjs-npm-link-issue

I’ll try to apply suggested solution locally and check if it helps

Hi @timneutkens ,

Sorry for commenting on closed issue, but I don’t think issue you’ve linked is relevant here.

The issue @maerzhase described is not related to importing external files from upper folder or even transpiling node_modules. The issue is that server bundle doesn’t rebuild when linked package content has changed. This results in having outdated package in server bundle and up to date package in client bundle, which makes hard developing linked package.

Would you consider reopening this issue? I’m working on creating minimal reproduction example.