Which Node.js built-in modules are compatible with Netlify Edge Functions?
0 reputation · 01 Dec 2022, 18:46 UTC
0 reputation · 01 Dec 2022, 18:46 UTC
Netlify Edge Functions utilize a Deno-based runtime to execute logic at the network edge, providing native support for TypeScript and JavaScript. This architecture allows for request interception and modification via middleware before a request reaches the origin.
Because the runtime is based on Deno rather than Node.js, there is a known discrepancy regarding the availability of standard Node.js built-in modules. When designing edge logic that requires specific system or network utilities, it is unclear which Node.js APIs are fully shimmed or natively supported within this environment.
33275 reputation · 02 Dec 2022, 02:59 UTC
Netlify Edge Functions run on Deno, not Node.js. Consequently, standard Node.js built-in modules are not natively available. However, compatibility is achieved through Deno's Node.js compatibility layer using the node: prefix.
Most common utility modules are compatible when imported with the explicit node: scheme. Common examples include:
node:path: For file and directory path manipulations.node:crypto: For cryptographic functions.node:events: For the EventEmitter class.node:util: For general-purpose utility functions.Modules that require direct access to the underlying operating system, low-level system calls, or C++ native addons are unsupported. This includes:
node:fs: The filesystem is read-only or unavailable in the edge runtime.node:child_process: Spawning shells or external processes is prohibited.node:http / node:https: These are superseded by the Web Standard Fetch API.To ensure maximum performance and stability at the edge, prefer Web Standard APIs or Deno's native implementation over Node shims:
| Node.js Module | Edge/Deno Alternative |
|---|---|
http / https |
fetch(), Request, Response |
crypto |
Web Crypto API (crypto.subtle) |
buffer |
Uint8Array or TextEncoder / TextDecoder |
To verify if a specific module is compatible with your current deployment target, use the following approach:
import module from 'node:module_name';Module not found or ReferenceError during the execution phase.Note: Please specify if you are using a specific version of a third-party library that depends on Node built-ins, as this may require a different bundling strategy (e.g., using an esbuild plugin).
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.