A supervisor program for running and monitoring Node.js applications with automatic restart capabilities
npx @tessl/cli install tessl/npm-supervisor@0.12.0Supervisor is a Node.js utility that monitors and automatically restarts applications when they crash or when source files change. It provides hot-code reloading functionality without memory leaks by completely restarting the process rather than using module reloading.
npm install supervisor -gAs a library:
const supervisor = require("supervisor");As a CLI tool (global installation):
supervisor myapp.js
# or
node-supervisor myapp.jsconst supervisor = require("supervisor");
// Run supervisor programmatically
supervisor.run(["--watch", "src", "--extensions", "js,json", "app.js"]);CLI usage:
# Basic supervision
supervisor myapp.js
# Watch specific directories and extensions
supervisor --watch src --extensions js,json myapp.js
# Debug mode with custom port
supervisor --debug=5858 myapp.js
# Non-interactive mode for production
supervisor --non-interactive --no-restart-on error myapp.jsSupervisor operates using several key components:
Primary function for starting supervisor programmatically.
/**
* Start supervisor with command-line arguments
* @param {string[]} args - Array of command-line arguments
*/
function run(args);Usage Example:
const supervisor = require("supervisor");
// Start with file watching
supervisor.run([
"--watch", "src,config",
"--extensions", "js,json,yaml",
"--ignore", "node_modules,logs",
"server.js"
]);Access to the currently running supervised process.
/**
* Reference to the current child process
* @type {ChildProcess|null}
*/
supervisor.child;Usage Example:
const supervisor = require("supervisor");
// Start supervisor
supervisor.run(["app.js"]);
// Access child process (after it starts)
setTimeout(() => {
if (supervisor.child) {
console.log("Child PID:", supervisor.child.pid);
// Send custom signal
supervisor.child.kill("SIGUSR1");
}
}, 1000);Comprehensive CLI for running supervisor as a command-line tool.
supervisor [options] <program>
supervisor [options] -- <program> [args...]Program and Execution:
<program> - The program to run (required)-x, --exec <executable> - Executable to run the program (default: 'node')File Watching:
-w, --watch <watchItems> - Comma-delimited list of folders/files to watch (default: '.')-i, --ignore <ignoreItems> - Comma-delimited list of folders to ignore-e, --extensions <extensions> - File extensions to watch (default: 'node,js')-p, --poll-interval <ms> - File polling interval in milliseconds--ignore-symlinks - Ignore symbolic links when watching--force-watch - Use fs.watch instead of fs.watchFileProcess Control:
-n, --no-restart-on <condition> - Don't restart on 'error'|'exit'|'success'-t, --non-interactive - Disable interactive stdin listening-k, --instant-kill - Use SIGKILL instead of SIGTERM for termination--save-pid <path> - Save supervisor's PID to fileDebug and Development:
--debug[=port] - Start with --debug flag--debug-brk[=port] - Start with --debug-brk flag--harmony - Start with --harmony flag--inspect - Start with --inspect flag--harmony_default_parameters - Enable harmony default parameters--harmony_destructuring - Enable harmony destructuringOutput and Logging:
-q, --quiet - Suppress debug messages-V, --verbose - Show extra debug messages-s, --timestamp - Log timestamp after each run-RV, --restart-verbose - Log files that cause restartsHelp:
-h, --help, -? - Display usage instructionsWhen running in interactive mode (default), supervisor accepts these stdin commands:
rs # Restart the supervised process manuallyUsage Examples:
# Basic usage
supervisor myapp.js
# CoffeeScript support
supervisor myapp.coffee
# Custom executable and extensions
supervisor -x coffee -e coffee,js myapp.coffee
# Production setup with specific watching
supervisor --watch lib,config --ignore logs,tmp --non-interactive server.js
# Debug mode
supervisor --debug=5858 --harmony myapp.js
# Full configuration example
supervisor \
--watch src,config \
--ignore node_modules,logs,tmp \
--extensions js,json,yaml \
--poll-interval 2000 \
--no-restart-on error \
--save-pid /var/run/myapp.pid \
--timestamp \
-- server.js --port 3000 --env productionnode,jscoffee,litcoffee when program has .coffee extension-e, --extensions option--force-watch forces fs.watch usage--poll-interval (default: 1000ms)--ignore-symlinks to skip symbolic linksSupervisor handles these system signals and forwards them to the child process:
// Handled signals (Unix/Linux)
SIGTERM // Graceful termination
SIGINT // Interrupt (Ctrl+C)
SIGHUP // Hangup
SIGQUIT // Quit signalBehavior:
--no-restart-on error: Don't restart on non-zero exit codes--no-restart-on exit: Don't restart regardless of exit code--no-restart-on success: Don't restart only on zero exit codesCore Node.js Modules:
fs - File system operationschild_process - Process spawning (spawn function)path - Path manipulation utilitiesExternal Dependencies: