LinuxCommandLibrary

live-server

Serve static web pages for development

TLDR

Serve an index.html file and reload on changes

$ live-server
copy

Specify a port (default is 8080) from which to serve a file
$ live-server --port=[8081]
copy

Specify a given file to serve
$ live-server --open=[about.html]
copy

Proxy all requests for ROUTE to URL
$ live-server --proxy=[/]:[http:localhost:3000]
copy

SYNOPSIS

live-server [options] [path]

PARAMETERS

path
    The directory to serve. Defaults to the current working directory if not specified.

-h, --help
    Displays the help message, showing all available options and their usage.

-v, --version
    Prints the installed version of live-server.

--port=PORT
    Specifies the port on which the server should run. Default is 8080.

--host=HOST
    Specifies the host IP address to bind the server to. Default is 0.0.0.0 (accessible from all network interfaces).

--no-browser
    Prevents live-server from automatically opening a new browser window/tab upon startup.

--browser=BROWSER
    Specifies which browser to open. Examples: 'chrome', 'firefox', 'google-chrome'.

--open={/PATH|false}
    Opens a specific path relative to the served directory, or disables opening the browser (equivalent to --no-browser when set to false).

--watch={DIR|file,DIR|file...}
    Comma-separated list of directories or files to watch for changes. By default, it watches all files in the served directory.

--ignore={DIR|file,DIR|file...}
    Comma-separated list of directories or files to ignore from watching. Useful for node_modules, .git, etc.

--file=PATH
    Serves a specific file as the entry point for all requests. Useful for single-page applications with client-side routing (e.g., --file=index.html).

--wait=MILLISECONDS
    Sets a delay in milliseconds before reloading the browser after a file change is detected. Useful if your build process takes time.

--spa
    Enables Single Page Application (SPA) mode. All requests will be redirected to the root index.html unless a file exists at the requested path. Similar to --file=index.html.

--cors
    Enables Cross-Origin Resource Sharing (CORS) headers for all responses, allowing requests from different origins.

--https
    Enables HTTPS (secure) serving. Requires --ssl-cert and --ssl-key options.

--ssl-cert=PATH
    Path to your SSL certificate file (e.g., server.crt). Required when --https is used.

--ssl-key=PATH
    Path to your SSL key file (e.g., server.key). Required when --https is used.

DESCRIPTION

live-server is a lightweight development server built on Node.js, designed specifically for rapid front-end web development. It automatically reloads your web browser whenever it detects changes in the files being served. This feature, known as "live reload," significantly streamlines the development workflow by eliminating the need for manual browser refreshes.

When invoked, live-server starts an HTTP server in the current directory (or a specified path), making your static HTML, CSS, and JavaScript files accessible via a local URL, typically http://127.0.0.1:8080. It then monitors these files for modifications. Upon saving changes to a file, live-server sends a signal to the browser, causing it to reload the page instantly, reflecting your updates in real-time. This real-time feedback loop is invaluable for designing layouts, styling components, and testing interactive features.

While not intended for production environments, live-server provides a robust set of options for customization, including specifying the port, host, default browser, ignoring certain files or directories, and even enabling HTTPS or CORS for more complex development scenarios. Its simplicity and effectiveness make it a popular choice for quick prototyping and local development of static websites or single-page applications.

CAVEATS

live-server is a development tool, not intended for production use due to its focus on convenience over security and performance optimized for serving a large number of concurrent requests. It primarily serves static files and offers limited server-side logic capabilities. It requires Node.js and npm (or yarn) to be installed on your system. While powerful for local development, its configuration is primarily command-line driven, which might be less flexible than dedicated web servers for more complex setups or advanced routing.

INSTALLATION

live-server is typically installed globally using Node.js's package manager, npm:
npm install -g live-server
Alternatively, you can install it locally as a development dependency for a project:
npm install --save-dev live-server and then run it via an npm script (e.g., "start": "live-server" in package.json).

BASIC USAGE EXAMPLES

To start a server in the current directory, serving files from ./ and opening your default browser:
live-server

To serve files from a specific directory, e.g., ./public:
live-server public/

To run on a specific port (e.g., 3000) and prevent the browser from opening automatically:
live-server --port=3000 --no-browser

To watch only specific files or directories and ignore others:
live-server --watch=index.html,css/style.css,js/ --ignore=node_modules/

HISTORY

live-server emerged within the Node.js ecosystem as a pragmatic solution to enhance front-end web development workflows. Historically, developers manually refreshed browsers after every code change, a tedious and time-consuming task. Building upon the concept of live reloading popularized by more integrated build systems, live-server was developed as a standalone, zero-configuration utility. Since its initial public release around 2014-2015, its development has prioritized simplicity, speed, and ease of use. It has rapidly become a fundamental tool for rapid prototyping and iterative development of static web content and single-page applications, leveraging WebSockets for efficient, real-time communication between the server and the client browser to trigger instant reloads upon file modifications.

SEE ALSO

http-server(1), serve(1), npm(1), node(1), python3 -m http.server, nginx(8), apache2(8)

Copied to clipboard