nodemon
Restart Node.js application on file changes
TLDR
Execute the specified file and watch a specific file for changes
Manually restart nodemon (note nodemon must already be active for this to work)
Ignore specific files
Pass arguments to the node application
Pass arguments to node itself if they're not nodemon arguments already (e.g. --inspect)
Run an arbitrary non-node script
Run a Python script
SYNOPSIS
nodemon [options] [your_script.js] [script_arguments]
nodemon [options] --exec [command] [command_arguments]
nodemon [options]
PARAMETERS
-w, --watch <dir>
Specify a directory or file to watch. Can be used multiple times.
-i, --ignore <pattern>
Ignore specified files or directories from being watched. Uses glob patterns. Can be used multiple times.
-e, --ext <extensions>
Specify file extensions to watch (e.g., js,pug,css). Defaults to js,mjs,json,coffee.
--exec <command>
Execute a specified command instead of 'node' (e.g., python app.py).
-d, --delay <milliseconds>
Delay the restart for a specified duration after a change is detected. Defaults to 1000ms.
-L, --legacy
Use the legacy 'polling' watch method, useful for NFS or Samba shares.
-V, --verbose
Set logging level to verbose, showing more internal information.
-q, --quiet
Suppress all logging output from nodemon itself, showing only script output.
--exitcrash
Exit nodemon if the child process crashes (useful for CI environments).
--signal <signal>
Specify the signal to send to the child process on restart (e.g., SIGUSR2). Defaults to SIGTERM.
-x, --script-restart <script>
Allows specifying a different script to run on restart. Deprecated, use --exec instead.
--config <file>
Specify a custom configuration file (default is nodemon.json).
-h, --help
Display help information and exit.
-v, --version
Display version information and exit.
DESCRIPTION
nodemon is a utility that monitors for any changes in your source code and automatically restarts your server, making development faster and more efficient. It is primarily used for Node.js based applications, but can be configured to run any command, making it useful for other languages and frameworks.
Instead of manually stopping and starting your Node.js application every time you make a change, nodemon listens for file system events (or polls for changes) and, upon detecting a modification, sends a signal to gracefully shut down the running process and then re-launches it.
This significantly streamlines the development workflow by providing immediate feedback on code changes without requiring manual intervention, allowing developers to focus more on coding and less on task management. It comes with flexible configuration options to ignore specific files or directories, watch only certain file extensions, and execute custom commands.
CAVEATS
While nodemon is highly effective, it might consume more CPU resources when watching a large number of files, especially when using the legacy polling method (--legacy or on network drives). It's crucial to properly configure --ignore to avoid unnecessary restarts caused by changes in temporary files or node_modules. In some environments, file system events might not be reliable, requiring the use of the polling option, which can be less efficient. Forceful termination (SIGKILL) might be used if the process doesn't shut down gracefully with SIGTERM, potentially leading to data loss if the application doesn't handle signals correctly.
CONFIGURATION FILES
nodemon can be configured via a nodemon.json file placed in your project root or specified via the --config flag. This allows for project-specific watch paths, ignore patterns, and executable commands to be defined centrally, reducing the need for lengthy command-line arguments. Global configurations can also be set in ~/.nodemon.json.
MANUAL RESTART
Even with automatic monitoring, there are times when you might want to force a restart. You can do this by typing rs (for restart) into the terminal where nodemon is running and pressing Enter. This provides immediate control over your development server.
DEFAULT BEHAVIOR
When no script is specified, nodemon will look for server.js, app.js, or main.js in the current directory and run it. By default, it automatically ignores common development directories like node_modules and .git.
HISTORY
nodemon was created by Remy Sharp to address the common pain point for Node.js developers: the need to manually restart the server after every code change. Before nodemon, developers would often use shell scripts or other tools to achieve similar functionality, but nodemon provided a dedicated, opinionated, and highly effective solution. It quickly became a standard tool in the Node.js development ecosystem due to its simplicity, ease of use, and significant productivity gains. Its continuous development and wide adoption have cemented its place as an indispensable utility for building robust and dynamic Node.js applications.