0
# Command Line Interface
1
2
Complete command-line interface for Live Server providing quick development server setup with extensive configuration options.
3
4
## Capabilities
5
6
### Basic Server Command
7
8
Start a live development server from the command line.
9
10
```bash { .api }
11
live-server [PATH] [OPTIONS]
12
13
# PATH: Optional directory to serve (defaults to current directory)
14
```
15
16
**Usage Examples:**
17
18
```bash
19
# Serve current directory on default port 8080
20
live-server
21
22
# Serve specific directory
23
live-server /path/to/project
24
live-server ./dist
25
live-server ../my-website
26
27
# Serve current directory with custom configuration
28
live-server --port=3000 --host=localhost
29
```
30
31
### Server Configuration Options
32
33
Configure server binding and behavior.
34
35
```bash { .api }
36
--port=NUMBER # Set server port (default: PORT env var or 8080)
37
--host=ADDRESS # Set host address (default: IP env var or 0.0.0.0)
38
--wait=MILLISECONDS # Wait time before reloading (default: 100ms)
39
--quiet | -q # Suppress logging (logLevel: 0)
40
--verbose | -V # Enable verbose logging (logLevel: 3)
41
```
42
43
**Usage Examples:**
44
45
```bash
46
# Custom port and host
47
live-server --port=3000 --host=127.0.0.1
48
49
# Random available port
50
live-server --port=0
51
52
# Reduce reload frequency
53
live-server --wait=500
54
55
# Quiet mode for automated scripts
56
live-server --quiet
57
58
# Verbose mode for debugging
59
live-server --verbose
60
```
61
62
### Browser Control Options
63
64
Control automatic browser launching and initial page.
65
66
```bash { .api }
67
--no-browser # Suppress automatic browser launching
68
--browser=BROWSER # Specify browser to use (comma-separated list)
69
--open=PATH # Launch browser to specific path instead of root
70
```
71
72
**Usage Examples:**
73
74
```bash
75
# Don't launch browser automatically
76
live-server --no-browser
77
78
# Use specific browser
79
live-server --browser=firefox
80
live-server --browser="google chrome"
81
82
# Open specific page
83
live-server --open=/dashboard
84
live-server --open=/admin/login
85
86
# Multiple pages
87
live-server --open=/page1 --open=/page2
88
89
# Use Chrome in incognito mode
90
live-server --browser="google chrome --incognito"
91
```
92
93
### File Watching Options
94
95
Configure which files to watch and ignore for live reload.
96
97
```bash { .api }
98
--watch=PATH # Comma-separated paths to exclusively watch
99
--ignore=PATH # Comma-separated paths to ignore
100
--ignorePattern=RGXP # Regular expression to ignore files (DEPRECATED)
101
--no-css-inject # Reload page on CSS changes instead of injecting
102
```
103
104
**Usage Examples:**
105
106
```bash
107
# Watch only specific directories
108
live-server --watch=src,public
109
110
# Ignore specific paths
111
live-server --ignore=node_modules,*.log,temp
112
113
# Ignore pattern (deprecated - use --ignore instead)
114
live-server --ignorePattern='.*\\.tmp$'
115
116
# Disable CSS injection - reload page on CSS changes
117
live-server --no-css-inject
118
```
119
120
### Single Page Application (SPA) Support
121
122
Configure server for Single Page Applications with client-side routing.
123
124
```bash { .api }
125
--spa # Enable SPA mode (redirect /path to /#/path)
126
--entry-file=PATH # Serve specific file for 404s (SPA fallback)
127
```
128
129
**Usage Examples:**
130
131
```bash
132
# Basic SPA mode - redirects to hash URLs
133
live-server --spa
134
135
# Serve index.html for all unmatched routes (common SPA pattern)
136
live-server --entry-file=index.html
137
138
# Serve app.html as fallback
139
live-server --entry-file=dist/app.html
140
141
# Combine SPA mode with entry file
142
live-server --spa --entry-file=index.html
143
```
144
145
### Advanced Routing and Proxying
146
147
Configure directory mounting and request proxying.
148
149
```bash { .api }
150
--mount=ROUTE:PATH # Mount directory to specific route
151
--proxy=ROUTE:URL # Proxy requests from route to URL
152
```
153
154
**Usage Examples:**
155
156
```bash
157
# Mount node_modules to /vendor route
158
live-server --mount=/vendor:./node_modules
159
160
# Mount multiple directories
161
live-server --mount=/components:./lib --mount=/assets:./static
162
163
# Proxy API requests to backend
164
live-server --proxy=/api:http://localhost:3001
165
166
# Proxy with full URL
167
live-server --proxy=/graphql:https://api.example.com/graphql
168
169
# Complex setup with mounting and proxying
170
live-server --mount=/static:./dist --proxy=/api:http://localhost:8000
171
```
172
173
### Security and Authentication
174
175
Configure HTTPS and HTTP Basic Authentication.
176
177
```bash { .api }
178
--https=PATH # Path to HTTPS configuration module
179
--https-module=NAME # Custom HTTPS module (e.g., 'spdy' for HTTP/2)
180
--htpasswd=PATH # Path to htpasswd file for Basic Auth
181
--cors # Enable CORS for any origin
182
```
183
184
**Usage Examples:**
185
186
```bash
187
# HTTPS with configuration file
188
live-server --https=./ssl-config.js
189
190
# HTTPS with HTTP/2 using spdy module
191
live-server --https=./ssl-config.js --https-module=spdy
192
193
# HTTP Basic Authentication
194
live-server --htpasswd=./users.htpasswd
195
196
# Enable CORS for cross-origin requests
197
live-server --cors
198
199
# Complete secure setup
200
live-server --https=./ssl.js --htpasswd=./auth --cors
201
```
202
203
Example HTTPS configuration file (`ssl-config.js`):
204
205
```javascript
206
const fs = require("fs");
207
208
module.exports = {
209
cert: fs.readFileSync(__dirname + "/server.cert"),
210
key: fs.readFileSync(__dirname + "/server.key"),
211
passphrase: "optional-passphrase"
212
};
213
```
214
215
### Middleware Integration
216
217
Add custom Connect-compatible middleware to the server stack.
218
219
```bash { .api }
220
--middleware=PATH # Path to .js file exporting middleware function
221
```
222
223
**Usage Examples:**
224
225
```bash
226
# Add custom middleware
227
live-server --middleware=./custom-middleware.js
228
229
# Use built-in middleware by name
230
live-server --middleware=spa
231
232
# Multiple middleware (order matters)
233
live-server --middleware=cors --middleware=./auth.js
234
```
235
236
Built-in middleware available by name:
237
- `spa` - Redirect routes to hash URLs
238
- `spa-ignore-assets` - SPA middleware that redirects routes to hash URLs except for requests with file extensions
239
- `example` - Example middleware (sets status 202)
240
241
Example custom middleware file:
242
243
```javascript
244
module.exports = function(req, res, next) {
245
// Add custom headers
246
res.setHeader('X-Custom-Header', 'live-server');
247
248
// Log requests
249
console.log(`${req.method} ${req.url}`);
250
251
next();
252
};
253
```
254
255
### Configuration File Support
256
257
Use configuration file for default options.
258
259
```bash { .api }
260
# Configuration loaded from ~/.live-server.json
261
```
262
263
Example `~/.live-server.json`:
264
265
```json
266
{
267
"port": 3000,
268
"host": "localhost",
269
"open": false,
270
"logLevel": 1,
271
"ignore": ["node_modules", "*.log"],
272
"middleware": ["cors"]
273
}
274
```
275
276
### Help and Version Information
277
278
Get help and version information.
279
280
```bash { .api }
281
--help | -h # Display usage help and exit
282
--version | -v # Display version and exit
283
```
284
285
**Usage Examples:**
286
287
```bash
288
# Show help
289
live-server --help
290
live-server -h
291
292
# Show version
293
live-server --version
294
live-server -v
295
```
296
297
### Environment Variables
298
299
Environment variables that affect default behavior.
300
301
```bash { .api }
302
PORT # Default port if --port not specified
303
IP # Default host if --host not specified
304
```
305
306
**Usage Examples:**
307
308
```bash
309
# Set defaults via environment
310
PORT=3000 IP=localhost live-server
311
312
# Override environment with command line
313
PORT=3000 live-server --port=4000 # Uses port 4000
314
```
315
316
## Complete Command Examples
317
318
Common real-world usage patterns:
319
320
```bash
321
# Development server for React/Vue SPA
322
live-server dist --spa --port=3000 --open=/
323
324
# Development with API proxy
325
live-server public --proxy=/api:http://localhost:8080 --cors
326
327
# HTTPS development server
328
live-server --https=./dev-ssl.js --port=8443
329
330
# Production-like testing with authentication
331
live-server dist --htpasswd=./users --no-css-inject --quiet
332
333
# Complex development setup
334
live-server src \
335
--port=3000 \
336
--proxy=/api:http://localhost:8000 \
337
--mount=/vendor:./node_modules \
338
--middleware=./dev-middleware.js \
339
--spa \
340
--cors \
341
--verbose
342
```