Error when deleting module file
I am getting the following error when deleting a module file:
{ Error: ENOENT: no such file or directory, open 'pathToModule.js'
at Object.openSync (fs.js:450:3)
at Object.readFileSync (fs.js:350:35)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:733:20)
at Object._extensions.(anonymous function) [as .js] (node_modules\hot-module-replacement\index.js:15:33)
at Module.load (internal/modules/cjs/loader.js:620:32)
at Watcher.<anonymous> (node_modules\hot-module-replacement\index.js:90:23)
at Watcher.emit (events.js:189:13)
at callback (node_modules\node-watch\lib\watch.js:254:14)
at node_modules\node-watch\lib\watch.js:112:10
at Array.forEach (<anonymous>)
errno: -4058,
syscall: 'open',
code: 'ENOENT',
path:
'pathToModule.js' }
There is no similar error when a module file is renamed.
Version: 3.0.2
Operating system: Windows
The same error is emitted when deleting a sub module.
In that case, I would probably except to get a warning like this instead:
(node:<number>) UnhandledPromiseRejectionWarning: Error: Cannot find module 'module'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:603:15)
at Function.Module._load (node_modules\hot-module-replacement\index.js:115:32)
at Module.require (internal/modules/cjs/loader.js:659:17)
at require (internal/modules/cjs/helpers.js:22:18)
at Object.<anonymous> (mainModule.js:1:81)
at Module._compile (internal/modules/cjs/loader.js:723:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:734:10)
at Object._extensions.(anonymous function) [as .js] (node_modules\hot-module-replacement\index.js:15:33)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
Renaming a sub module also works, but in that case I would probably expect to get an error like the above as well.
What do you think desired behaviour should be?
The reason error is thrown is because it tries to re-load module in watch() handler
The same error is emitted when deleting a sub module.
yes, when you mark a module as 'hot-reloadable' by accepting it it watches all dependencies of that module
Need to investigate if webpack api has any way of handling errors. Currently "accept" handler is only notified when module can be loaded after it's updated ( and by the time handler is called module is already in the cache ). For example if you delete file, but then re-create it (with valid content) your handler will be called again for new file
I think we could check the eventType and check if it's value is 'remove' and if so then we don't try to reload that module, because it is no longer exists. So we don't get this error.
https://github.com/sidorares/hot-module-replacement/blob/8fa1925acac17a10f53f6bfde510efc5c49d3da9/index.js#L70
not sure if it's a good idea or not. You skip updating the tree but it's now in the invalid state and changing any parent module of the one removed would still display similar message
API-wise we could try to wire addStatusHandler to allow consumers to react on various errors and transitions of a module to and from errored state
https://webpack.js.org/api/hot-module-replacement/#addstatushandler
I agree with you that it is not necessarily a good idea. (In my use case if you delete a top module that is completely normal.)
Currently changing any parent module of the one removed does not emits any warning, but I think it should as I mentioned it before. https://github.com/sidorares/hot-module-replacement/issues/8#issuecomment-445986246