connect-livereload is a Connect/Express middleware designed to inject the LiveReload script directly into HTML responses. This eliminates the need for a browser extension, simplifying the development workflow by automatically reloading the browser upon file changes detected by companion tools like `grunt-contrib-watch`. The current stable version is 0.6.1, which was published approximately 7 years ago (December 2018), indicating that the package is largely unmaintained. Its primary differentiation is the server-side injection, which contrasts with client-side browser extensions. It does not, however, serve the `livereload.js` script itself, requiring another module (e.g., a LiveReload server like `node-livereload`) to provide it. Configuration options allow for specifying the LiveReload server port, ignoring certain file types (e.g., `.js`, `.css`), or customizing the HTML detection and injection rules.
npm install connect-livereloadVerified import paths — ran on the pinned version, not inferred.
This example sets up an Express server with `connect-livereload` and a separate `livereload` server. It demonstrates how to integrate the middleware to inject the LiveReload script into static and dynamic HTML responses, enabling automatic browser reloads upon file changes. It highlights correct middleware placement and the necessity of a separate LiveReload server.
Evaluate modern build tools (e.g., Vite, Webpack Dev Server with HMR) or actively maintained LiveReload server implementations. If you must use it, be aware of potential compatibility issues with newer Node.js or Express versions.
Ensure a LiveReload server is running and configured to serve `livereload.js` on the port specified in `connect-livereload` options (default 35729). For example, `const lrserver = livereload.createServer(); lrserver.watch(__dirname + '/public');`.
Disable any browser LiveReload extensions when using `connect-livereload` to avoid conflicts.
For static HTML injection: `app.use(connectLiveReload(...)); app.use(express.static(...));`. For dynamic HTML: `app.use(express.static(...)); app.use(connectLiveReload(...)); app.get('/dynamic', ...);`.Start your LiveReload server (e.g., `livereload.createServer().watch(...)`) and ensure it's configured to listen on port 35729 (or the port specified in `connect-livereload` options). Verify no other process is blocking this port.
1. Disable browser LiveReload extensions. 2. Verify `connect-livereload` is placed correctly in the middleware stack (before static for HTML). 3. Check `ignore` and `include` options to ensure desired files are not excluded. 4. Ensure your LiveReload server (`livereload` package or similar) is actively watching the correct directories and communicating with the browser.
Ensure `connect-livereload` is only processing text-based HTML responses. Review your middleware stack and `ignore` rules. If you have custom middleware modifying `res.write`/`res.end`, ensure compatibility. You can also customize the `html` option to better suit your content detection if non-standard responses are being processed.