Five Server
Five Server is a modern development server with live reload capabilities, designed as a maintained fork of the popular `live-server` package. It is completely rewritten in TypeScript and aims to provide up-to-date dependencies and enhanced features compared to its predecessor. Currently at version 0.4.5, it offers features like remote logs for debugging on mobile devices, built-in PHP server support, and compatibility with Server Side Rendered applications (e.g., Express.js). While it ships with a CLI, it also offers a programmatic API for integration into projects. Key differentiators include its robust module import options for various JavaScript environments and features specifically for its VSCode extension, and it targets Node.js version 16 or higher.
Common errors
-
TypeError: FiveServer is not a constructor
cause Attempting to instantiate `FiveServer` in CommonJS by directly requiring the package without accessing its default export (e.g., `require('five-server')`).fixCorrect the import to `const FiveServer = require('five-server').default;` -
SyntaxError: Named export 'FiveServer' not found. The requested module 'five-server' does not provide an export named 'FiveServer'
cause Incorrectly using a named import for a default export, or attempting `import FiveServer from 'five-server'` in a Node.js ESM environment where the direct root import might not resolve correctly.fixUse the explicit ESM path: `import FiveServer from 'five-server/esm.mjs';` or fall back to: `import pkg from 'five-server'; const { default: FiveServer } = pkg;` -
Error: listen EADDRINUSE: address already in use :::8080
cause Another process on your system is already using the port that Five Server is trying to bind to (e.g., port 8080).fixChange the port number in your configuration (e.g., `five-server --port 8081` in CLI, or `{ port: 8081 }` in your programmatic options) or identify and terminate the conflicting process.
Warnings
- breaking Five Server is a complete rewrite and maintained fork of `live-server`. Migrating from `live-server` requires uninstalling the old package (`npm -g rm live-server`) and installing `five-server`. The API for programmatic usage has changed significantly.
- gotcha Advanced features like 'Instant Updates', 'Highlights', and 'Auto Navigation' are exclusively available through the Five Server VSCode Extension, not via the standalone CLI or programmatic API.
- breaking Five Server requires Node.js version 16 or higher. Older Node.js versions are not supported, and attempting to run it on unsupported versions will result in errors.
- gotcha Due to varied module resolver behaviors across JavaScript environments, explicit import paths or `.default` access might be required for programmatic usage in Node.js ESM or CommonJS. Simply importing from 'five-server' might not always work directly.
Install
-
npm install five-server -
yarn add five-server -
pnpm add five-server
Imports
- FiveServer
const FiveServer = require('five-server')import FiveServer from 'five-server'
- FiveServer
import FiveServer from 'five-server'
import FiveServer from 'five-server/esm.mjs'
- FiveServer
const FiveServer = require('five-server')const FiveServer = require('five-server').default
Quickstart
import FiveServer from 'five-server';
import path from 'path';
import fs from 'fs';
// Create a dummy 'public' directory and an index.html file for demonstration
const publicDir = path.join(process.cwd(), 'public');
if (!fs.existsSync(publicDir)) {
fs.mkdirSync(publicDir, { recursive: true });
}
fs.writeFileSync(path.join(publicDir, 'index.html'), '<h1>Hello Five Server!</h1><p>Watch me reload.</p>', 'utf8');
// Example: Serve the 'public' directory on port 8080 without opening a browser automatically.
const server = new FiveServer();
server.start({
port: 8080,
root: publicDir, // Set the document root to the 'public' directory
open: false, // Do not open browser automatically
watch: true, // Enable live reload
dir: publicDir // Ensure 'dir' is also set for correct root behavior
}).then(() => {
console.log('Five Server started on http://localhost:8080');
console.log(`Watching for changes in: ${publicDir}`);
console.log('Open your browser to http://localhost:8080');
}).catch(err => {
console.error('Failed to start Five Server:', err);
});