Registry / devops / fetch-to-node

fetch-to-node

JSON →
library2.1.0jsnpmunverified

Convert between fetch-compatible Request/Response objects and Node.js http.IncomingMessage/ServerResponse for use in WinterTC runtimes (Cloudflare Workers, Bun, Deno, Fastly Compute). v2.1.0 is stable, actively maintained, and ships TypeScript definitions. Unlike alternatives like @fastly/http-compute-js, it has zero runtime dependencies beyond Node.js compatibility shims (Readable, Writable, Buffer). Ideal for running Express or other Node.js HTTP middleware in edge environments.

npm install fetch-to-node
INSTALL
IMPORT
SIG · FETCH-TO-NODE
F
fetch-to-node
devopsjavascriptv2.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

toReqRes
import { toReqRes } from 'fetch-to-node'
const toReqRes = require('fetch-to-node').toReqRes
The library is ESM-only. CommonJS require is not officially supported; use dynamic import if needed.
toFetchResponse
import { toFetchResponse } from 'fetch-to-node'
import toFetchResponse from 'fetch-to-node'
Default export does not exist; always use named import.
Request (type import for TS)
import type { Request } from 'fetch-to-node'
TypeScript types are exported if you need to reference the underlying types (though they are well-known DOM types).

Shows the core usage: converts a fetch Request to Node.js req/res objects, writes a response using Node.js API, and converts back to a fetch Response.

import { toReqRes, toFetchResponse } from 'fetch-to-node'; export default { async fetch(request: Request): Promise<Response> { const { req, res } = toReqRes(request); res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ data: 'Hello World!' })); return await toFetchResponse(res); }, };
Debug
Known issues
breakingtoFetchResponse returns a Promise that resolves when res emits 'finish'. If res.end() is never called, the promise hangs.
fix
Always call res.end() after writing headers and body, or use a timeout.
affects: >=2.0.0
deprecatedHTTP version is hardcoded to 1.1 – HTTP/2 is not supported.
fix
No workaround; use a different library if HTTP/2 is required.
affects: >=1.0.0
gotchaCalling toReqRes() on a Request whose body has already been consumed will cause an error.
fix
Clone the Request if its body is needed elsewhere, or read body before passing to toReqRes.
affects: >=1.0.0
gotchaThe 'socket' property on the IncomingMessage may not be available in all runtimes.
fix
Avoid relying on socket properties; for example, req.socket.remoteAddress may be undefined.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: Readable.from is not a function
Node.js Readable stream platform support missing (e.g., older Cloudflare Workers without nodejs_compat flag)
fix
Enable the `nodejs_compat` flag in Cloudflare Workers (wrangler.toml: `nodejs_compat = true`).
Unhandled Promise Rejection: Error: The 'finish' event did not fire
res.end() not called before awaiting toFetchResponse()
fix
Ensure res.end() is called after writing all data.
Cannot find module 'fetch-to-node'
Incorrect package installation or ESM/CJS mismatch in environment
fix
Install with `npm install fetch-to-node`. Use `import` syntax (ESM) as the library does not provide a CommonJS entry.
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies
node:streamoptionalUses Readable and Writable streams internally
node:bufferoptionalUses Buffer for binary data handling
Agent activity
2 hits · last 30 days
node
2
Resources
fetch-to-node — npm install fetch-to-node · libregistry