nodemon.json
Overview
The `nodemon.json` file is a configuration file used by **Nodemon**, a utility that monitors changes in source files and automatically restarts the Node.js application during development. This file specifies how Nodemon should behave for the particular project, defining which files or directories to watch or ignore, the file extensions that trigger restarts, and the command to execute when changes are detected.
This configuration optimizes the development workflow by ensuring that the application rebuilds and restarts only when relevant source files change, improving efficiency and reducing unnecessary restarts.
Configuration Properties
The file is a JSON object with the following properties:
Property | Type | Description |
|---|---|---|
`ignore` | Array of strings | Specifies file(s) or directory paths that Nodemon should ignore and not watch for changes. |
`watch` | Array of strings | Specifies file(s) or directory paths that Nodemon should watch for changes. |
`ext` | String | Specifies the file extensions that Nodemon should monitor for changes. |
`exec` | String | The command to execute when a watched file changes. |
Detailed Explanation of Properties
ignore
Type: Array of strings
Purpose: Prevents Nodemon from restarting the app when files matching these paths change.
Current value:
["src/routes.ts"]Usage: This ignores changes in the specific file
src/routes.ts. This is useful if changes to this file do not require a restart or if it causes unnecessary reloads.
watch
Type: Array of strings
Purpose: Specifies which files or directories Nodemon should monitor for changes.
Current value:
[ "../../../packages/*/dist/tsconfig.tsbuildinfo", "../../common/api/dist/tsconfig.tsbuildinfo", "src" ]Explanation:
The watcher monitors build info files (
tsconfig.tsbuildinfo) in certain package directories to detect TypeScript build changes.It also watches the
srcdirectory for source code changes.
Usage: Watching these files ensures Nodemon restarts the app when relevant compiled files or source files are updated.
ext
Type: String
Purpose: Defines the file extensions that Nodemon should monitor for changes.
Current value:
"ts"Usage: Nodemon will only trigger restarts when
.ts(TypeScript) files change, ignoring other file types.
exec
Type: String
Purpose: The command run by Nodemon upon detecting a file change.
Current value:
"yarn build && node dist/ethereum/api/src/app.js"Explanation:
First, it runs
yarn buildto compile the TypeScript project.Then, it starts the Node.js server by running the compiled JavaScript file located at
dist/ethereum/api/src/app.js.
Usage: This ensures the app always runs the latest build after changes.
Usage Example
When running Nodemon with this configuration, the developer would typically execute:
nodemon
Nodemon will:
Watch all
.tsfiles inside thesrcdirectory and the specified build info files.Ignore changes in
src/routes.ts.On relevant changes, run
yarn buildto compile the project.Start or restart the Node.js server by running the compiled app at
dist/ethereum/api/src/app.js.
This setup is particularly useful in a TypeScript monorepo or multi-package workspace, where build artifacts and incremental builds are important to track.
Implementation Details and Notes
Watching
tsconfig.tsbuildinfofiles:
These files are incremental build info files generated by TypeScript. Watching them allows Nodemon to restart the process when the build state changes, even if source files don't change directly. This is useful in complex monorepos where compiled outputs affect runtime behavior.Ignoring specific source files:
Sometimes certain source files change frequently but don't require a restart or cause undesirable reloads. Ignoring them prevents unnecessary rebuilds, optimizing development speed.Sequential command execution:
Theexeccommand chainsyarn buildand then runs the compiled app withnode. This guarantees the app runs the latest compiled code. However, ifyarn buildfails, the app will not start, preventing running outdated or invalid code.
Interaction with the System
Build system:
The file assumes the use ofyarnas the package manager and a build script namedbuildthat compiles TypeScript sources.Runtime:
Runs the compiled JavaScript applicationdist/ethereum/api/src/app.js.Development workflow:
Nodemon uses this config to monitor relevant files and streamline the rebuild and restart cycle during local development.Monorepo structure:
The paths inwatchpoint to relative directories outside the current folder, indicating this project is part of a larger monorepo with shared packages and APIs.
Mermaid Diagram
The diagram below illustrates the workflow of this `nodemon.json` configuration during development:
flowchart TD
A[File Change Detected] --> B{Is file ignored?}
B -- Yes --> C[No Restart]
B -- No --> D{Is file extension '.ts'?}
D -- No --> C
D -- Yes --> E{Is file in watch list?}
E -- No --> C
E -- Yes --> F[Run "yarn build"]
F --> G{Build successful?}
G -- No --> H[Do not restart app]
G -- Yes --> I[Run "node dist/ethereum/api/src/app.js"]
I --> J[App Restarted]
Summary
nodemon.jsondefines how Nodemon watches and restarts the Node.js application during development.It watches
.tsfiles in specified directories, ignoringsrc/routes.ts.On change, it runs a build command and starts the compiled app.
This configuration is tailored for a TypeScript monorepo environment, improving developer productivity by automating rebuilds and restarts efficiently.
If you want to customize the behavior, you can adjust the paths in `watch` and `ignore`, change the file extensions in `ext`, or modify the `exec` command to fit your build and launch scripts.