serve
Serve static files over HTTP
TLDR
Start an HTTP server listening on the default port to serve the current directory
Start an HTTP server on a specific [p]ort to serve a specific directory
Start an HTTP server with CORS enabled by including the Access-Control-Allow-Origin: * header in all responses
Start an HTTP server on the default port rewriting all not-found requests to the index.html file
Start an HTTPS server on the default port using the specified certificate
Start an HTTP server on the default port using a specific configuration file
Display help
SYNOPSIS
serve [path] [options]
serve -l <port> [options]
serve -s [options]
PARAMETERS
[path]
The directory to serve. Defaults to the current working directory (.).
-l, --listen <port>
Specifies the port on which the server should listen. Defaults to 3000 or an available port.
-p, --port <port>
An alias for --listen.
-s, --single
Enables single-page application (SPA) mode. All non-existent paths will be redirected to index.html.
-d, --debug
Enables debug mode, providing more verbose output for troubleshooting.
-c, --config <file>
Specifies a custom configuration file (e.g., serve.json) instead of the default.
-C, --cors
Enables Cross-Origin Resource Sharing (CORS) for all routes, allowing requests from different origins.
-o, --open
Automatically opens the server URL in your default web browser upon startup.
-n, --no-clipboard
Prevents copying the server URL to the system clipboard.
-S, --no-etag
Disables ETag support, which is used for client-side caching.
-H, --no-compression
Disables Gzip and Brotli compression for served files.
-h, --help
Displays help information and available options for the command.
-v, --version
Displays the current version number of the serve utility.
DESCRIPTION
serve is a powerful, zero-configuration command-line HTTP server designed for quickly serving static files, single-page applications (SPAs), and directory listings. Developed by Vercel, it's widely adopted in the Node.js ecosystem for local development, testing build outputs, or sharing files over a network.
It automatically handles common web server features such as Gzip/Brotli compression, ETag caching, and clean URL redirects (e.g., serving index.html for directory requests). Its simplicity makes it ideal for front-end development with frameworks like React, Vue, or Angular, providing an instant web server without complex setup. serve requires Node.js and is typically installed globally via npm or yarn.
CAVEATS
serve is not a standard, built-in Linux command; it must be installed separately via Node.js's package manager (npm or yarn). It is primarily designed for development and simple static file hosting, and while robust, it's generally not recommended for high-traffic, production-grade deployments without a proper reverse proxy (like Nginx) in front of it.
INSTALLATION
serve is typically installed globally on your system using npm or yarn:
npm install -g serve
or
yarn global add serve
This makes the serve command available for use in any directory.
CONFIGURATION FILE
For more advanced or persistent configurations, serve supports a serve.json file. Placed in the root of your served directory, this file allows you to define custom rewrites, redirects, headers, environment variables, and more, providing fine-grained control over how assets are served without needing complex command-line arguments every time.
HISTORY
serve was originally created by Vercel (formerly Zeit) as part of their ecosystem for deploying web applications, with a focus on static sites and serverless functions. It quickly gained popularity for its 'zero-config' approach and efficiency in providing a local development server. Its ongoing development ensures support for modern web features and performance improvements, cementing its role as a go-to tool for web developers.
SEE ALSO
http-server(1), python3 -m http.server(1), nginx(8), apache2(8)