liveBook · Manning

Code reloading

As explained in Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform

A development technique that detects source-code changes and automatically restarts or reloads a running application.

Code reloading shortens the time between saving a change and seeing its result. During development, a watcher detects source changes and restarts the affected microservice automatically. This is a development and testing workflow, not a reason to run a watcher in production. section 2.6.8

For a Node.js microservice, start with a runnable application:

  • Install nodemon as a development dependency: `npm install --save-dev nodemon`. It need not be installed in production. section 2.6.8
  • Keep `npm start` for the ordinary `node src/index.js` command. Add a `start:dev` npm script that runs `nodemon src/index.js`, then use `npm run start:dev` while editing. section 2.6.8
  • If file changes are not detected under WSL2 or a virtual machine, use `nodemon --legacy-watch ./src/index.js`. This polls for changes instead of relying on filesystem watching; omit it elsewhere for slightly better performance. section 5.5.2
Listing 5.2 Setting up package.json for live reload with nodemon
{
  "name": "history",
  "version": "1.0.0",
  "description": "",
  "main": "./src/index.js",
  "scripts": {
    "start": "node ./src/index.js",
    "start:dev": 
     "nodemon --legacy-watch ./src/index.js"   #1
  },
  --snip--  "dependencies": {
    "express": "^5.0.0-beta.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.21"
  }
}
#1 Uses nodemon to enable live reload for this microservice. When the code changes, nodemon automatically restarts the microservice.

The package configuration keeps the ordinary start command separate from the nodemon-based development command. section 5.5.2

Containers add a second requirement: the changed code must reach the running container. Code baked into a Docker image cannot be updated by editing the host copy. In development, Docker volumes share code between the computer and containers; nodemon can then restart the relevant microservice without rebuilding and restarting the entire application. Apply the same setup to each microservice managed by Docker Compose. Immutable images remain useful for production deployment and software-supply-chain verification. section 5.5 section 5.11

Check the feedback loop

Start the application with `docker compose up --build`. Change the history microservice’s startup message from “Hello world!” to “Hello computer!” and save. Its new message should appear in the Compose output; the unchanged video-streaming microservice should not restart. Try introducing an error, inspect the output, then fix and save the file to trigger another restart. section 5.5.5

The same feedback-loop idea applies to tests. `npx jest --watchAll` reruns all tests when code changes. With Git, `npx jest --watch` uses changed-file information for better performance. A project may expose test watching through a `test:watch` npm script. section 9.5.6 section 10.6.1

How Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform covers it

How deep it goes
Works through nodemon installation, npm scripts, container code sharing, a reload check, and a file-watching workaround.
Examples
Uses a startup-message edit in a Compose application and Jest commands that rerun tests after code changes.
What it assumes
Assumes a runnable Node.js microservice and npm; introduces nodemon setup before extending reloading to Docker Compose.
What you can do after
Operate/configure it with nodemon and Docker Compose so edited microservices restart, and use Jest watch mode for test feedback.
How it connects
Connects development dependencies and npm scripts with Docker volumes, Docker Compose, container images, and Jest watch mode.

Also explained in

Compare how 4 books explain code reloading →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Bootstrapping Microservices, Second Edition: With Docker, Kubernetes, GitHub Actions, and Terraform. The section links point to the parts of the book it draws on.