Auto-Restart Node.js Apps: A Nodemon Setup Guide
Tired of manually restarting your Node.js server every time you make a small change? Learn how to automate this process with Nodemon and streamline your development workflow. This guide provides a step-by-step approach to installing, configuring, and using Nodemon to automatically restart your Node.js applications.
Why Use Nodemon for Automatic Restarts?
In Node.js development, changes to your code require restarting the server to take effect. This manual process becomes tedious and time-consuming. Nodemon automates this by watching your file system and restarting your Node.js application whenever it detects a change. This speeds up development and lets you focus on coding.
Quickstart: Auto-Restart Node.js with Nodemon
Here’s how to get started with Nodemon:
- Install Nodemon: Install either globally or locally within your project.
- Set up a Node.js project: Create a simple server file (e.g.,
server.js
). - Run Nodemon: Start your application using the
nodemon
command. - Make changes: Edit your server file and watch Nodemon automatically restart the server.
Step 1: Installing Nodemon Globally or Locally
You can install Nodemon globally or locally, depending on your preference and project needs.
Global Installation
- Benefit: Accessible from any project on your system.
- Command (npm):
npm install nodemon --global
- Command (yarn):
yarn global add nodemon
Local Installation
- Benefit: Project-specific dependency management.
- Command (npm):
npm install nodemon --save-dev
- Command (yarn):
yarn add nodemon --dev
- Usage: Execute using
./node_modules/.bin/nodemon [your node app]
or through npm scripts.
Note: Local installation might require using npx nodemon
or defining a script in your package.json
.
Step 2: Setting Up a Simple Express Project with Nodemon
Let's create a basic Express server to see Nodemon in action. This example will display a message when the server starts, and we'll modify it to trigger an automatic restart.
Create server.js
Run with Nodemon
- Command:
nodemon server.js
- Expected Output:
[nodemon] 2.0.15 [nodemon] to restart at any time, enter `rs` [nodemon] watching path(s): *.* [nodemon] watching extensions: js,mjs,json [nodemon] starting `node server.js` Dolphin app listening on port 3000!
Test Automatic Restart
- Modify the message in
server.js
(e.g., change "Dolphin" to "Shark"). - Save the file.
- Nodemon will automatically restart the server, and you'll see the updated message in the console.
Step 3: Customize Nodemon with Command-Line Options
Nodemon provides several command-line options for customization. Here are a few useful examples:
--exec
: Specify the executable to use (e.g.,--exec ts-node
for TypeScript).--ext
: Define file extensions to watch (e.g.,--ext js,ts
).--delay
: Set a delay before restarting (e.g.,--delay 3.2
for 3.2 seconds).--watch
: Specify directories or files to watch (e.g.,--watch server
).--ignore
: Exclude files or directories from being watched (e.g.,--ignore '*.test.js'
).--verbose
: Display verbose output.
Example: Combining Options
nodemon --watch server --ext ts --exec ts-node --ignore '*.test.ts' --delay 3 server/server.ts
Step 4: Configure Nodemon Using nodemon.json
For complex configurations, use a nodemon.json
file. This keeps your command clean and makes your settings reusable.
Create nodemon.json
Alternative: package.json
You can also embed the configurations directly within your package.json
under the nodemonConfig
key.
Run Nodemon with Configuration
Once your configuration file is set up, simply run:
nodemon server/server.ts
Nodemon will automatically pick up and apply the settings from your configuration file.
Conclusion: Streamline Node.js Development with Nodemon
Nodemon significantly improves your Node.js development workflow by automating server restarts. Whether you choose command-line options or a configuration file, Nodemon helps you focus on coding and building great applications. Explore the official Nodemon documentation for advanced features.