hot-module-replacement icon indicating copy to clipboard operation
hot-module-replacement copied to clipboard

Once an error occurs, file can't be updated

Open ericclemmons opened this issue 6 years ago • 4 comments

I was able to replicate via:

  1. module.hot.accept a file that can be successfully required.
  2. Make changes to the file (confirmed updated).
  3. Add require('lodash') or any other dependency that doesn't exist.
  4. Error occurs here: https://github.com/sidorares/hot-module-replacement/blob/2af226b9e8fd1ce75b40b96c0920f053e92db450/index.js#L115
  5. Subsequent changes to the file aren't reloaded.
🚀 Ready! http://localhost:3000/
🔁  Hot-reloaded /private/tmp/test/routes/index.js
{ Error: Cannot find module 'lodash'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:580:15)
    at Function.Module._load (/Users/eric/Projects/ericclemmons/polydev/node_modules/hot-module-replacement/index.js:115:32)
    at Module.require (internal/modules/cjs/loader.js:636:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/private/tmp/test/routes/index.js:1:86)
    at Module._compile (internal/modules/cjs/loader.js:688:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
    at Object._extensions.(anonymous function) [as .js] (/Users/eric/Projects/ericclemmons/polydev/node_modules/hot-module-replacement/index.js:15:33)
    at Module.load (internal/modules/cjs/loader.js:598:32)
    at Watcher.<anonymous> (/Users/eric/Projects/ericclemmons/polydev/node_modules/hot-module-replacement/index.js:90:23) code: 'MODULE_NOT_FOUND' }
Accepted /private/tmp/test/routes/index.js
Accepted /private/tmp/test/routes/index.js
Accepted /private/tmp/test/routes/index.js

Not sure how, but I would expect that changes to a file that has been accepted would be watched for changes even if it can't successfully be required.

ericclemmons avatar Mar 04 '19 06:03 ericclemmons

Hmmm, just went through replicating & it looks like I may have some broken logic elsewhere.

Sorry!

ericclemmons avatar Mar 04 '19 06:03 ericclemmons

Ok, so maybe the real question is how can I perform an action when HMR fails?

module.hot.accept only fires when a module can be successfully replaced, but if there's an error & I want to perform some cleanup or notification, there doesn't seem to be a hook.

ericclemmons avatar Mar 04 '19 06:03 ericclemmons

Ok, so maybe the real question is how can I perform an action when HMR fails?

not sure yet. To begin, I need to check what api webpack HMR follows

There is self-accept handler that takes error callback ( https://webpack.js.org/api/hot-module-replacement/#accept-self ) Also onErrored in appy - https://webpack.js.org/api/hot-module-replacement/#apply

If you have time maybe can you point me to some webpack HMR error screens integration examples and we'll figure out best way to handle errors from users code in this module

sidorares avatar Mar 04 '19 07:03 sidorares

Here's a sandbox showing the error I want to trap in handler.js: https://codesandbox.io/s/k2nvx9n3x5

ericclemmons avatar Mar 10 '19 17:03 ericclemmons