esbuild-server
esbuild-server is a development server specifically designed to integrate with esbuild for rapid, lightweight asset bundling and serving. It features zero runtime dependencies beyond esbuild itself, providing live reload, API proxying with dynamic rewrite capabilities, SPA support via History API fallback, and static asset serving. The current stable version is `0.3.0`, with a release cadence that addresses bug fixes and introduces new features like HTTPS support. It differentiates itself by tightly coupling with esbuild's build process, offering a minimal setup, and being fully typed for TypeScript environments, making it an efficient choice for projects already leveraging esbuild.
Common errors
-
Error: Cannot find module 'esbuild'
cause The `esbuild` package, a peer dependency, has not been installed.fixRun `npm install esbuild --save-dev` or `yarn add esbuild --dev`. -
Error: listen EADDRINUSE: address already in use :::8080
cause Another process is already using the specified port (defaulting to 8080).fixChange the `port` option in `serverOptions` to an available port, e.g., `{ port: 3000 }`. -
TypeError: createServer is not a function
cause Attempting to call `require('esbuild-server')` directly as a function, or using an incorrect import style in an ESM context.fixFor CommonJS, use `const { createServer } = require('esbuild-server');`. For ESM, use `import { createServer } from 'esbuild-server';`. -
Error: ENODATA: no such file or directory, read 'server.key'
cause The HTTPS key or certificate file specified in `https` options could not be found or read.fixVerify that the paths to `key` and `cert` files are correct and that the Node.js process has read permissions for these files. Ensure the files actually exist at the specified location.
Warnings
- breaking esbuild-server versions prior to `0.2.0` are not compatible with esbuild `0.17.0` and newer due to API changes in esbuild. Attempting to use older esbuild-server with modern esbuild will likely result in build failures.
- gotcha Using the `https` option requires providing valid SSL key and certificate files. Without correctly formatted and accessible files (e.g., using `fs.readFileSync`), the server will fail to start with HTTPS enabled.
- gotcha The `esbuild` package is a peer dependency of `esbuild-server` and must be installed explicitly. Failing to install `esbuild` will result in a 'Cannot find module' error when `esbuild-server` attempts to initialize.
- gotcha By default, `esbuild-server` attempts to open a browser window upon starting the server if `open: true` is set in `serverOptions`. This can be disruptive in CI/CD environments or headless setups.
Install
-
npm install esbuild-server -
yarn add esbuild-server -
pnpm add esbuild-server
Imports
- createServer
import esbuildServer from 'esbuild-server'; esbuildServer.createServer();
import { createServer } from 'esbuild-server'; - createServer (CommonJS)
const createServer = require('esbuild-server'); createServer.createServer();const { createServer } = require('esbuild-server'); - Server instance
createServer(esbuildOptions, serverOptions).listen();
const server = createServer(esbuildOptions, serverOptions); server.start();
Quickstart
import { createServer } from 'esbuild-server';
import path from 'path';
const rootDir = process.cwd();
createServer(
{
bundle: true,
entryPoints: [path.join(rootDir, 'src/app.ts')],
outdir: path.join(rootDir, 'dist'),
// Example: process.env.NODE_ENV !== 'production' ? 'development' : 'production'
define: { 'process.env.NODE_ENV': JSON.stringify('development') }
},
{
static: path.join(rootDir, 'public'), // Assumes 'public' directory with 'index.html'
port: 8080,
historyApiFallback: true, // Useful for SPAs
injectLiveReload: true,
open: true, // Opens browser automatically
proxy: {
'/api': 'http://localhost:3001' // Example API proxy
},
// https: {
// key: fs.readFileSync('server.key'),
// cert: fs.readFileSync('server.crt'),
// }
}
).start();
console.log('esbuild-server started on http://localhost:8080');