LiveReload JavaScript Client
LiveReload.js is the client-side JavaScript implementation of the LiveReload protocol, designed to automatically refresh web browsers during development when file changes are detected by a compatible LiveReload server. The current stable version is 4.0.2, with releases occurring periodically for bug fixes and dependency updates. Unlike many development tools, this package is explicitly *not* recommended for direct installation or bundling by most end-users; instead, it is intended to be served and managed by a LiveReload server (e.g., LiveReload app for Mac, rack-livereload, guard-livereload, grunt-contrib-watch, python-livereload). This approach ensures compatibility and allows for server-specific customizations. It supports live CSS and image reloading, as well as full page reloads for other file types, communicating with the server via WebSockets. It differentiates itself by being solely the client aspect of a larger ecosystem, expecting a server component to drive its functionality.
Common errors
-
ReferenceError: window is not defined
cause Attempting to run `livereload-js` in a Node.js environment or a non-browser context.fixThis script is designed for browsers. Ensure it's included in an HTML page or a client-side bundle that executes in a browser environment. If you need server-side LiveReload, use a dedicated Node.js LiveReload *server* package. -
Failed to load resource: net::ERR_CONNECTION_REFUSED (or similar connection errors)
cause The `livereload.js` client script failed to connect to the LiveReload server, often due to the server not running, incorrect host/port configuration, or a firewall blocking the connection.fixVerify your LiveReload server is running and listening on the expected port (default 35729). Check the script tag's `src` URL for correct host and port. Ensure no firewalls are blocking connections to the LiveReload port. -
LiveReload is not defined
cause The `livereload.js` script was not loaded or executed successfully before other scripts attempted to interact with its global object (though direct interaction with `LiveReload` global is less common now, it attaches functionality to `window`).fixConfirm the `<script>` tag loading `livereload.js` is correctly placed and accessible (e.g., `http://localhost:35729/livereload.js`). Check browser developer tools for network errors during script loading. Ensure there are no JavaScript errors preventing its execution.
Warnings
- gotcha It is strongly recommended to use the `livereload.js` script bundled with your specific LiveReload server/app/tool, rather than installing this package directly via npm or Bower. The server-bundled version is guaranteed to be compatible and may be customized.
- breaking Explicitly leaving the port parameter blank in the script URL (e.g., `?port=`) now derives the port from the host part. In previous versions, this would effectively set the port to 0.
- gotcha `livereload-js` is a browser-side client and relies on browser globals (`window`, `document`). It will not run directly in a Node.js environment without a browser emulation layer.
- gotcha This package is a development-time dependency and should not be shipped to production. Its purpose is to facilitate live reloading during local development.
Install
-
npm install livereload-js -
yarn add livereload-js -
pnpm add livereload-js
Imports
- script tag inclusion
<!-- Add this snippet to your HTML, replacing the host/port as needed --> <script>document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js"></' + 'script>')</script> - Browserify/CommonJS require
import LiveReload from 'livereload-js';
window.LiveReloadOptions = { host: 'localhost', port: 35729 }; require('livereload-js'); - LiveReloadConnect / LiveReloadDisconnect events
document.addEventListener('LiveReloadConnect', () => console.log('LiveReload connected!')); document.addEventListener('LiveReloadDisconnect', () => console.log('LiveReload disconnected.'));
Quickstart
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>LiveReload Demo</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Hello LiveReload!</h1>
<p>Watch this page refresh automatically.</p>
<!-- Recommended way to include livereload.js, served by your dev server -->
<script>
// This script snippet is typically injected by a LiveReload server or build tool.
// It tries to connect to the LiveReload server, usually on port 35729.
// Replace 'localhost' and '35729' if your server uses different settings.
document.write('<script src="http://' + (location.host || 'localhost').split(':')[0] + ':35729/livereload.js"></' + 'script>');
</script>
</body>
</html>
/* style.css */
body {
font-family: sans-serif;
background-color: lightblue;
color: #333;
}
h1 {
color: darkblue;
}