0
# Global Polyfills
1
2
Install web standard global APIs in Node.js environments to enable compatibility with web-standard code.
3
4
## Capabilities
5
6
### Install Globals
7
8
Installs global polyfills for web standards in Node.js environments using either undici or @remix-run/web-fetch.
9
10
```typescript { .api }
11
/**
12
* Install web standard global APIs in Node.js environments
13
* @param options - Configuration options
14
* @param options.nativeFetch - If true, use undici's native fetch; otherwise use @remix-run/web-fetch (default: false)
15
*/
16
function installGlobals(options?: { nativeFetch?: boolean }): void;
17
```
18
19
**Installed Globals:**
20
21
- `global.fetch` - Fetch API for making HTTP requests
22
- `global.Request` - Request constructor for creating request objects
23
- `global.Response` - Response constructor for creating response objects
24
- `global.Headers` - Headers constructor for managing HTTP headers
25
- `global.FormData` - FormData constructor for handling form data
26
- `global.File` - File constructor for file handling
27
28
**Note:** ReadableStream and WritableStream are declared in TypeScript types for better IDE support but are not polyfilled by this function.
29
30
**Usage Examples:**
31
32
```typescript
33
import { installGlobals } from "@remix-run/node";
34
35
// Install globals using @remix-run/web-fetch (default)
36
installGlobals();
37
38
// Now you can use web standard APIs
39
const response = await fetch("https://api.example.com/data");
40
const data = await response.json();
41
42
// Install globals using undici's native fetch
43
installGlobals({ nativeFetch: true });
44
45
// Use undici's faster native implementation
46
const response = await fetch("https://api.example.com/data");
47
```
48
49
**Implementation Details:**
50
51
When `nativeFetch: false` (default), uses `@remix-run/web-fetch` polyfills:
52
- Provides consistent behavior across Node.js versions
53
- Full compatibility with web standards
54
- Suitable for most applications
55
56
When `nativeFetch: true`, uses undici's native implementations:
57
- Better performance for high-throughput applications
58
- Native Node.js implementation
59
- Requires Node.js 18+ for optimal compatibility
60
61
**Environment Configuration:**
62
63
The function extends Node.js global types to include web standard APIs:
64
65
```typescript { .api }
66
declare global {
67
namespace NodeJS {
68
interface Global {
69
File: typeof File;
70
Headers: typeof Headers;
71
Request: typeof Request;
72
Response: typeof Response;
73
fetch: typeof fetch;
74
FormData: typeof FormData;
75
ReadableStream: typeof ReadableStream;
76
WritableStream: typeof WritableStream;
77
}
78
}
79
80
interface RequestInit {
81
duplex?: "half";
82
}
83
}
84
```
85
86
**Note:** The TypeScript declarations include ReadableStream and WritableStream for IDE support, but only fetch, Request, Response, Headers, FormData, and File are actually installed as globals.