Gulp Live Server
gulp-live-server is a lightweight Gulp plugin designed to provide a development server with integrated live-reloading capabilities for static assets or Node.js applications. It abstracts the complexities of setting up a web server (potentially using `connect` or `express` internally) and a separate LiveReload server (via `tiny-lr`), allowing developers to quickly spin up a server and have their browser automatically refresh upon file changes. The package offers methods for serving static files, running a custom Node.js script as the server, and notifying the browser of updates. Despite its utility, the package is largely unmaintained, with its last release (version 0.0.31) dating back over 7 years to July 2017. Users should be aware of potential compatibility issues with newer Node.js versions or modern Gulp setups, especially those using ES Modules.
Common errors
-
TypeError: Bad argument at TypeError (native) at ChildProcess.spawn
cause The `server.start` method's `this` context is lost when bound directly in `gulp.watch`.fixWrap the bound function in an anonymous function: `gulp.watch('myapp.js', function() { server.start.bind(server)() });` -
ERR_REQUIRE_ESM
cause Attempting to `require()` this CommonJS-only package in an ES Module context (e.g., a file with `"type": "module"` in `package.json` or a `.mjs` extension).fixEnsure your `gulpfile.js` is a CommonJS file (plain `.js` without `"type": "module"` in `package.json` or a separate `cjs` folder for Gulp tasks) and use `const gls = require('gulp-live-server');`. -
ReferenceError: gulp is not defined
cause The `gulp` object was not correctly imported or available in the scope where `gulp-live-server` is used.fixEnsure `var gulp = require('gulp');` is present at the top of your `gulpfile.js` and `gulp` is installed as a development dependency (`npm install --save-dev gulp`).
Warnings
- breaking This package is CommonJS-only and does not support ES Modules. Attempting to import it directly in an ESM context (e.g., `"type": "module"` in `package.json` or `.mjs` files) will result in import errors.
- gotcha When restarting the server via `gulp.watch`, directly binding `server.start` might lead to a `TypeError: Bad argument` due to how `ChildProcess.spawn` handles the `this` context.
- gotcha This package is effectively abandoned, with its last release over 7 years ago. It may contain unpatched security vulnerabilities, suffer from compatibility issues with newer Node.js versions or dependencies, and is not actively maintained. Consider modern alternatives.
Install
-
npm install gulp-live-server -
yarn add gulp-live-server -
pnpm add gulp-live-server
Imports
- gls
import gls from 'gulp-live-server';
const gls = require('gulp-live-server'); - gls.static
import { static } from 'gulp-live-server';const server = gls.static('public'); - gls.new
import { new } from 'gulp-live-server';const server = gls.new('myapp.js');
Quickstart
const gulp = require('gulp');
const gls = require('gulp-live-server');
gulp.task('serve-static', function() {
// 1. Serve with default settings (serves 'public' folder on port 3000)
const server = gls.static();
server.start();
// 2. Serve at a custom port for 'dist' folder
const customPortServer = gls.static('dist', 8888);
customPortServer.start();
// 3. Serve multiple folders
const multiFolderServer = gls.static(['dist', '.tmp']);
multiFolderServer.start();
// Use gulp.watch to trigger server actions (notify, start or stop)
gulp.watch(['static/**/*.css', 'static/**/*.html'], function (file) {
server.notify.apply(server, [file]);
customPortServer.notify.apply(customPortServer, [file]);
multiFolderServer.notify.apply(multiFolderServer, [file]);
});
});
// To run: `gulp serve-static`