Middleware to watch files and remove
require.cache on change, making nodemon unnecessary.
Sometimes restarting the entire process when a file changes (as with nodemon) takes too long or is undesirable. This is often the case with "universal" web applications, where the code which runs both on the client and server is already hot-reloaded with webpack. In such apps we sometimes have server-only code (such as a GraphQL server or a RESTful API). Restarting the entire server with nodemon would mean forcing webpack to have to take time to rebuild, loosing the benefits of hot reloading.
This module saves that time by specifically only removing files from the
(a cache that
require() uses on every call) when a change to one of the files on the
file system is detected.
const createWatcher = ;const watchMiddleware =;//...const app = ; // or express()// Now is the trickier bit, you'll have to `require()` all of the files that your// local middleware or router depend on. This is because if we required it on top,// the router / middleware would always refere to that instance and there would be// no way of clearing it.app;app;app;
Run your process with the
NODE_DEBUG=connect-chokidar environment variable set.