nodemon.json
Overview
The `nodemon.json` file is a configuration file used by **Nodemon**, a utility that monitors for any changes in your source files and automatically restarts your Node.js application. This file defines specific rules and parameters that control how Nodemon behaves during development, including which files to watch for changes, which files to ignore, the file extensions Nodemon should monitor, and the command to execute when restarting the application.
This configuration is crucial for streamlining the development workflow by automating rebuilds and restarts, thereby improving productivity and reducing manual intervention.
Configuration Properties and Their Purpose
The `nodemon.json` file contains a JSON object with the following keys:
Property | Type | Description |
|---|---|---|
`ignore` | Array of strings | Specifies files or directories that Nodemon should **not** watch for changes. Changes in these files will **not** trigger a restart. |
`watch` | Array of strings | Specifies additional files or directories that Nodemon should watch for changes. Nodemon will restart the app when files in these locations change. |
`ext` | String | Defines the file extensions Nodemon should monitor for changes. Only files with these extensions will trigger a restart. |
`exec` | String | The command that Nodemon will execute when restarting the application. This typically includes build commands and the start command for the app. |
Detailed Explanation of Each Property
1. ignore
"ignore": ["src/routes.ts"]
Purpose: To exclude specific files from Nodemon’s watch list.
In this file:
src/routes.tsis ignored, meaning changes to this file will not cause Nodemon to restart the application.Use case: This is often used to prevent restarts on files that change frequently but do not require a server restart, or to avoid unnecessary rebuilds.
2. watch
"watch": ["../../coinstacks/common/api/dist/tsconfig.tsbuildinfo", "src"]
Purpose: To explicitly specify files or directories that Nodemon should monitor.
In this file: Nodemon watches the
tsconfig.tsbuildinfofile in a shared or common directory (../../coinstacks/common/api/dist) as well as the localsrcdirectory.Use case: Useful when the project depends on files outside the default directories or when you want to monitor build artifacts and source code simultaneously.
3. ext
"ext": "ts"
Purpose: To specify which file extensions to watch.
In this file: Only files with the
.ts(TypeScript) extension are monitored.Use case: Prevents unnecessary restarts triggered by file changes irrelevant to the application (e.g.,
.mdfiles,.json, etc.).
4. exec
"exec": "yarn build && node dist/proxy/api/src/app.js"
Purpose: Defines the command to execute when Nodemon restarts the application.
In this file: On restart, Nodemon runs
yarn buildto compile the TypeScript project and then starts the Node.js application by running the compiled JavaScript file atdist/proxy/api/src/app.js.Use case: Ensures the project is rebuilt before launching the updated app, which is essential for TypeScript projects that require transpilation.
Usage Example
You place `nodemon.json` in the root directory of your project. Then, running `nodemon` in the terminal will automatically use this configuration.
nodemon
When you make changes to any `.ts` files inside `src` or the specified `tsconfig.tsbuildinfo` file, Nodemon will:
Run
yarn buildto compile TypeScript.Start the app with Node.js using the compiled output
dist/proxy/api/src/app.js.Ignore changes in
src/routes.tsto avoid unnecessary restarts.
Important Implementation Details
Integration with TypeScript Build Process:
Theexeccommand incorporates the build step (yarn build) to ensure that the latest TypeScript source code is compiled before running the Node.js app. This is critical for ensuring that the running code reflects the current source state.Selective Watching and Ignoring:
The combination ofwatchandignoreprovides fine control over which files trigger restarts. Ignoringsrc/routes.tsmight be deliberate, perhaps because this file changes frequently but does not require server restarts, or it is handled differently.Cross-Directory Watching:
Watching a file outside the project directory (../../coinstacks/common/api/dist/tsconfig.tsbuildinfo) suggests this project depends on a shared or common module, and changes in that dependency should also trigger restarts.
Interaction with Other Parts of the System
Build System (
yarn build):
Nodemon depends on the build process managed by Yarn to compile TypeScript files. This means that the build scripts inpackage.jsonmust be properly configured for the project.Compiled Output (
dist/proxy/api/src/app.js):
This is the entry point for the Node.js application after the TypeScript code is compiled. Changes in source files trigger a rebuild and restart of this compiled app.Shared Dependencies:
Watching thetsconfig.tsbuildinfofile in a common directory indicates that changes in shared configuration or build metadata propagate to this project, ensuring synchronization.
Visual Diagram: Workflow of nodemon.json Configuration
flowchart TD
A[File Change Detected]
B{File Extension is .ts?}
C{File in ignore list?}
D{File in watch list?}
E[Ignore Change - No Restart]
F[Run Command: yarn build && node dist/proxy/api/src/app.js]
G[Restart Application]
A --> B
B -- No --> E
B -- Yes --> C
C -- Yes --> E
C -- No --> D
D -- Yes --> F
D -- No --> E
F --> G
**Explanation:**
Nodemon detects file changes.
It checks if the file extension matches
.ts.It verifies if the file is in the ignore list.
If it passes these checks and is in the watch list, Nodemon runs the build and restart commands.
Otherwise, the change is ignored.
Summary
The `nodemon.json` file is a lightweight configuration file that enables efficient development of a TypeScript Node.js application by:
Watching specific
.tsfiles and directories for changes.Ignoring files that do not require a restart.
Running a build step before restarting the app.
Supporting cross-project dependencies by watching shared build metadata.
This configuration helps maintain a smooth developer experience by automating rebuilds and restarts while avoiding unnecessary interruptions.