env-runner

0.1.7 · active · verified Sun Apr 19

env-runner is a generic environment runner for JavaScript applications, abstracting away the complexities of various runtime environments. It enables developers to run server applications across Node.js worker threads, child processes, Bun, Deno, Cloudflare Workers (via Miniflare), Vercel, Netlify, or even in-process. The package provides essential features like hot-reloading for development, WebSocket proxying, and a bidirectional messaging system between the main process and the runner environment. Currently at version 0.1.7, it is actively developed with rapid minor releases focusing on enhancements and new runner integrations, offering a unified API for deploying serverless functions or local servers across diverse JavaScript ecosystems. Its key differentiator is providing a consistent interface regardless of the underlying runtime.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates setting up `EnvServer` to run an application entry point with hot-reloading and proxy requests through a standard HTTP server. Remember to create an `app.ts` file with a default `fetch` handler.

import { serve } from "srvx";
import { EnvServer } from "env-runner";
import { fileURLToPath } from 'node:url';
import path from 'node:path';

// app.ts (your application entry point - create this file)
// export default {
//   fetch(request: Request) {
//     return new Response(`Hello from env-runner at ${new Date().toISOString()}!`);
//   },
// };

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const appEntryPath = path.join(__dirname, 'app.ts'); // Path to your app.ts entry

const envServer = new EnvServer({
  runner: "node-process", // Choose your desired runner: 'miniflare', 'bun-process', etc.
  entry: appEntryPath,
  watch: true,
  watchPaths: [path.join(__dirname, 'src')], // Example additional watch path
});

envServer.onReady((_runner, address) => {
  if (address) {
    console.log(`Worker ready on ${address.host}:${address.port}`);
  } else {
    console.log("Worker ready, but no address reported.");
  }
});

envServer.onReload(() => {
  console.log("Application reloaded!");
});

envServer.onError((error) => {
  console.error("EnvServer error:", error);
});

await envServer.start();

// Use with any HTTP server (srvx is used here as an example from the README)
const server = serve({
  fetch: (request) => envServer.fetch(request),
});

const port = process.env.PORT ? parseInt(process.env.PORT) : 3000;
server.listen({ port, host: 'localhost' });
console.log(`HTTP server listening on http://localhost:${port}`);

// Graceful shutdown
process.on('SIGINT', async () => {
  console.log('Shutting down env-runner and HTTP server...');
  await envServer.close();
  server.close();
  process.exit(0);
});

view raw JSON →