There are already node "wrappers" that handle watching for file changes and restarting your application (such as node-supervisor), as well as reloading on crash, but I wasn't fond of having that. Piping adds "hot reloading" functionality to node, watching all your project files and reloading when anything changes, without requiring a "wrapper" binary.
Piping uses the node cluster API to spawn your application in a thread and then kill/reload it when necessary. Piping should not be used in production.
npm install piping
Piping is super simple to integrate and should not change the way you run your application:
;// Application logic hereexpress = ;app = ;app;
With the default settings, this will cause a second instance of your application to be launched, which is monitored by the first. Piping then does a trick with uncaughtException handling to avoid running any of your code on the first process.
The function returned by piping also accepts an options object. The following options are supported:
- main (string): The path to the "top" file of your application. Defaults to
process.argv, which should be sufficient provided you launch your application via "node yourapp.js". Other launch methods may require this to be set manually. If your app doesn't reload/reloads when it shouldn't, try changing this.
- args (string): Arguments to pass to your application. Defaults to the current arguments.
- hook (boolean): Whether to hook into node's "require" function and only watch required files. Defaults to false, which means piping will watch all the files in the folder in which main resides. The require hook can only detect files required after invoking this module!
- includeModules (boolean): Whether to include required files than reside in node_modules folders. Defaults to false. Only has an effect when hook is true. For ignoring node_modules when hook is false, please use ignore.
- ignore (regex): Files/paths matching this regex will not be watched. Defaults to
- respawnOnExit (boolean) : Default false. Whether the application should respawn after exiting. If you experience problems with infinite loops, try setting this to false.
- throw (boolean): Use the trick with exceptions to avoid running your code in the first instance. Defaults to true. If set to false, you will need to use an if statement around the piping call to tell if your code should run. Piping returns a truthy value in this case.
- quiet (boolean): Suppress piping output. Defaults to false.
- usePolling (boolean) : From chokidar. Default false. Whether to use fs.watchFile (backed by polling), or fs.watch. It is typically necessary to set this to true to successfully watch files over a network.
- interval (integer) : From chokidar. Polling specific. Interval of file system polling (default 100).
- binaryInterval (integer) : From chokidar. Polling specific. Interval of file system polling for binary files.
main: "./app/server.js" hook:true;// App logic
Piping can also be used just by passing a string. In this case, the string is taken to be the "main" option:
"./app/server.js";// App logic
Piping emits lifecycle events on both the supervisor and your application process, allowing for custom behavior. Most events provide the reloaders status, in the form:
exiting: boolean // True if the process is about to reloadfirstRun: boolean // True on the first run of the applicationexitReason: string? // Reason for last exit, one of "exited", "errored", "killed", "requested"fileChanged: string? // Name of file which changed and triggered the last reload.}
- exited: The last application process exited of its own accord, with 0 error code.
- errored: The last application process exited with a non zero exit code.
- killed: The last application process has to be killed in order to reload.
- requested: The last application process reloaded cleanly as requested.
You can receive lifecycle events by passing in a function as the second parameter to the piping call, or the first if you are not specifying any options. This function will be called once everything is ready, and will be passed an EventEmitter. See the example below for supported events:
On the application process, the piping call will return an EventEmitter. See the example below for supported events:
const reloader = ;reloader;reloader;