nodemon

repository·main·Indexed 12 days ago

https://github.com/remy/nodemon

A development tool that automatically restarts Node.js applications when file changes are detected. It acts as a transparent wrapper around the node command and can be configured via nodemon.json or package.json. Beyond Node.js, it can monitor and execute other programs using the --exec flag and execMap.

Tokens
13.5K
Snippets
79
Records
89
Agent score
96%

What's inside nodemon

  1. Understand nodemon's option parsing precedence

    main

    When you run nodemon, it determines which file to execute and which configuration to apply by following a specific order of precedence. If multiple sources define the same option, the source higher in this list takes priority:

    1. CLI arguments: Any flags or arguments passed directly to the nodemon command.
    2. Local config: Configuration found in a local configuration file (e.g., nodemon.json).
    3. Global config: Configuration found in your global nodemon configuration.
    4. package.json (main): The main field in your project's package.json.
    5. package.json (start): The start field in your project's package.json.
    6. index.js: The default fallback to index.js in the current directory.
  2. Use variables in nodemon configuration files

    main

    When using a .json configuration file, you cannot use shell backticks for command substitution. Instead, you must use the supported template variables:

    • {{pwd}}: The current working directory.
    • {{filename}}: The filename passed to nodemon.

    These are useful for constructing dynamic exec commands.

    {
      "exec": "processing --sketch={{pwd}} --run"
    }
  3. Understand the nodemon execution flow

    main

    The internal execution flow of nodemon follows this sequence:

    1. The CLI receives input.
    2. The parser processes the input into nodemon options.
    3. These options are converted into rules.
    4. The rules are used to configure the environment, watch for changes, and finally start the process.
  4. Run non-node scripts with nodemon

    main

    nodemon can monitor and execute programs other than Node.js by using the --exec flag. It will automatically monitor the file extension of the script being run.

    Example: Running Python:

    nodemon --exec "python -v" ./app.py

    This runs app.py with Python in verbose mode and monitors .py files.

    Defining default executables via execMap: You can map file extensions to specific executables in your nodemon.json to avoid using --exec every time.

    Example nodemon.json for Perl support:

    {
      "execMap": {
        "pl": "perl"
      }
    }

    Now you can simply run nodemon script.pl.

    nodemon --exec "python -v" ./app.py
  5. Delay application restarts

    main

    If you are performing bulk file operations (like uploading many files), nodemon might restart too many times. Use the --delay flag to throttle restarts.

    CLI Usage:

    • Seconds: nodemon --delay 10 server.js
    • Milliseconds (float): nodemon --delay 2.5 server.js
    • Milliseconds (explicit): nodemon --delay 2500ms server.js

    Config File Usage: In nodemon.json, the delay value is always interpreted in milliseconds.

    {
      "delay": 2500
    }
    nodemon --delay 2500ms server.js
  6. Suppress the support message

    main

    To quieten nodemon (useful for CI environments or if you are already supporting the project), set the SUPPRESS_SUPPORT environment variable to 1.

    export SUPPRESS_SUPPORT=1
  7. Install nodemon

    main

    You can install nodemon globally to your system path using npm or yarn, or as a development dependency for a specific project.

    Global installation (recommended for CLI use):

    npm install -g nodemon
    # or
    yarn global add nodemon

    Local development dependency:

    npm install --save-dev nodemon
    # or
    yarn add nodemon -D

    If installed locally, you cannot run nodemon directly from the command line. Instead, use npx nodemon or call it from within an npm script (e.g., npm start).

    npm install -g nodemon
  8. Basic usage of nodemon

    main

    To run a script with nodemon, use the command nodemon [script.js]. If you omit the script name, nodemon will attempt to read the main field from your package.json. By default, nodemon monitors .js, .mjs, .coffee, .litcoffee, and .json files.

    $ nodemon server.js
  9. Configure nodemon using nodemon.json

    main

    You can configure nodemon behavior using a nodemon.json configuration file in your project root. This allows you to define restart commands, file extensions to watch, directories to ignore, environment variables, and custom events.

    {
      "restartable": "rs",
      "ignore": [
        ".git",
        "node_modules/**/node_modules"
      ],
      "verbose": true,
      "execMap": {
        "js": "node --harmony"
      },
      "events": {
        "restart": "osascript -e 'display notification "App restarted due to:\n'$FILENAME'" with title "nodemon"'"
      },
      "watch": [
        "test/fixtures/",
        "test/samples/"
      ],
      "env": {
        "NODE_ENV": "development"
      },
      "ext": "js,json"
    }
  10. Install nodemon with permission issues

    main

    If you encounter EACCES errors during a global installation (often requiring sudo), try adding the --unsafe-perm flag to the npm install command.

    sudo npm install -g nodemon --unsafe-perm
  11. Gracefully reload scripts with custom signals

    main

    You can instruct nodemon to send a specific signal to your application process tree using the --signal flag. This is useful for implementing graceful reloads.

    Example CLI usage:

    nodemon --signal SIGHUP server.js

    Example Application implementation (Node.js):

    process.on("SIGHUP", function () {
      reloadSomeConfiguration();
      process.kill(process.pid, "SIGTERM");
    })

    Note: The signal is sent to every process in the process tree. If using cluster, you may need to handle signal forwarding in the master process.

    nodemon --signal SIGHUP server.js