Registry / web-framework / edge-runtime

edge-runtime

JSON →
library4.0.1jsnpmunverified

edge-runtime is a JavaScript/TypeScript library, currently at version 4.0.1, developed by Vercel. It provides a robust, spec-compliant environment for simulating Vercel Edge Functions or any Web-standard compliant JavaScript code within Node.js or a CLI. This enables local development, testing, and debugging of Edge Functions without requiring actual deployment, significantly streamlining the development workflow. The library aims to faithfully replicate the Edge Function environment, offering Web APIs like `fetch`, `Request`, `Response`, and `URL` as globals within its isolated context. It maintains an active release cadence, with major versions typically aligning with Node.js LTS updates or substantial API enhancements, as demonstrated by the v4.0.0 release which raised the minimum Node.js version requirement. Its core differentiation lies in providing a high-fidelity simulation of the Vercel Edge context.

npm install edge-runtime
INSTALL
IMPORT
SIG · EDGE-RUNTIME
E
edge-runtime
web-frameworkjavascriptv4.0.1
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

EdgeRuntime
import { EdgeRuntime } from 'edge-runtime';
const { EdgeRuntime } = require('edge-runtime');
The primary class for creating a simulated Edge environment. ESM is the preferred and often only reliable import method in modern Node.js versions compatible with this library.
EdgeRuntimeOptions
import type { EdgeRuntimeOptions } from 'edge-runtime';
Type definition for configuring the EdgeRuntime instance, useful for TypeScript projects.
Request
const request = new runtime.Request('https://example.com');
import { Request } from 'edge-runtime';
Web APIs like `Request`, `Response`, `URL`, and `fetch` are exposed as globals within the `EdgeRuntime` instance and should not be imported directly from the package. Access them via the runtime instance or within evaluated code.

Demonstrates initializing an EdgeRuntime instance, injecting global variables, evaluating Edge Function code, and simulating a fetch event to receive a response.

import { EdgeRuntime } from 'edge-runtime'; async function runEdgeFunctionSimulation() { // Instantiate the Edge Runtime, optionally providing initial globals const runtime = new EdgeRuntime({ initialGlobals: { // Simulate environment variables or other global objects MY_SECRET_KEY: process.env.MY_SECRET_KEY ?? 'default-secret', SOME_CONFIG: { foo: 'bar' } } }); // Define your Edge Function code as a string const edgeFunctionCode = ` addEventListener('fetch', event => { const url = new URL(event.request.url); if (url.pathname === '/api/greet') { event.respondWith(new Response('Hello from the Edge!', { status: 200, headers: { 'content-type': 'text/plain' } })); } else if (url.pathname === '/api/config') { event.respondWith(new Response(JSON.stringify(SOME_CONFIG), { status: 200, headers: { 'content-type': 'application/json' } })); } else if (url.pathname === '/api/secret') { event.respondWith(new Response(MY_SECRET_KEY, { status: 200, headers: { 'content-type': 'text/plain' } })); } else { event.respondWith(new Response('Not Found', { status: 404 })); } }); `; // Evaluate the Edge Function code within the simulated runtime runtime.evaluate(edgeFunctionCode); // Simulate an incoming fetch request const request1 = new runtime.Request('https://example.com/api/greet'); const response1 = await runtime.dispatchFetch(request1); console.log('Response 1 Status:', response1.status); console.log('Response 1 Body:', await response1.text()); const request2 = new runtime.Request('https://example.com/api/config'); const response2 = await runtime.dispatchFetch(request2); console.log('Response 2 Status:', response2.status); console.log('Response 2 Body:', await response2.json()); const request3 = new runtime.Request('https://example.com/api/secret'); const response3 = await runtime.dispatchFetch(request3); console.log('Response 3 Status:', response3.status); console.log('Response 3 Body:', await response3.text()); } runEdgeFunctionSimulation().catch(console.error);
edge-runtime --version
Debug
Known issues
breakingVersion 4.0.0 of `edge-runtime` dropped support for Node.js 16. Using the package with Node.js versions older than 18 will result in errors.
fix
Upgrade your Node.js environment to version 18 or higher (LTS recommended) or pin `edge-runtime` to a compatible major version (e.g., `^3.0.0` for Node.js 16 compatibility).
affects: >=4.0.0
gotchaThe `EdgeRuntime` creates an isolated environment. Node.js built-in modules (e.g., `fs`, `path`) and externally installed Node.js packages are not directly accessible within the code evaluated by `runtime.evaluate()` unless explicitly passed as `initialGlobals` or polyfilled.
fix
For Node.js specific functionalities, perform operations outside the runtime. If certain values or functions are needed inside, pass them via the `initialGlobals` option during `EdgeRuntime` instantiation, ensuring they are serializable or compatible with the Edge environment.
affects: >=1.0.0
gotchaWeb APIs like `fetch`, `Request`, `Response`, `URL`, `console`, etc., are available as globals within the code evaluated by the EdgeRuntime. Attempting to `import` these directly from `edge-runtime` or other libraries will lead to incorrect behavior or `undefined` errors.
fix
Access Web APIs as global objects directly within the code passed to `runtime.evaluate()` or through the `runtime` instance itself (e.g., `new runtime.Request()`, `await runtime.dispatchFetch()`) for external interaction.
affects: >=1.0.0
Errors
Common errors & fixes
Error: The 'EdgeRuntime' class is not a constructor
Attempting to use `require()` for `EdgeRuntime` in an environment where ESM is expected or configured for the package, or incorrect import syntax.
fix
Ensure you are using `import { EdgeRuntime } from 'edge-runtime';` for ESM contexts. If stuck on CommonJS, you might need to configure your build system or Node.js environment for ESM compatibility or target an older major version of `edge-runtime` if one exists with CJS support.
ReferenceError: fetch is not defined
Trying to use the `fetch` global outside of the `EdgeRuntime` context in a standard Node.js environment, or within evaluated code before the runtime is properly initialized.
fix
Ensure `fetch` calls are made within the JavaScript code string passed to `runtime.evaluate()`, or use `await runtime.dispatchFetch(request)` to simulate an external fetch event against the runtime.
Node.js version 16 is not supported by edge-runtime@4.x. Please upgrade to Node.js >=18.
Running `edge-runtime` version 4.x or higher on an incompatible Node.js version (specifically 16 or lower).
fix
Upgrade your Node.js installation to version 18 or later. Alternatively, if Node.js 16 support is critical, you must use `edge-runtime` version `^3.0.0` or earlier.
Upgrade
Version history
4.0.1latest on npm
Audit
Dependencies
@edge-runtime/ponyfillrequiredProvides Web API polyfills and ponyfills for the runtime environment.
@edge-runtime/formatrequiredInternal utility for code formatting within the runtime.
@edge-runtime/vmrequiredCore VM implementation for executing code in an isolated context.
@edge-runtime/primitivesrequiredFundamental Web API primitives required by the runtime.
Agent activity
4 hits · last 30 days
node
4
Resources