0
# @remix-run/node
1
2
@remix-run/node provides Node.js-specific platform abstractions and utilities for Remix applications. It offers essential Node.js runtime features including file-based session storage, cookie management, stream utilities for handling readable/writable streams, file upload handlers for multipart form data processing, and global polyfills for web standards in Node.js environments.
3
4
## Package Information
5
6
- **Package Name**: @remix-run/node
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install @remix-run/node`
10
11
## Core Imports
12
13
```typescript
14
import {
15
createFileSessionStorage,
16
unstable_createFileUploadHandler,
17
installGlobals,
18
writeReadableStreamToWritable,
19
createRequestHandler,
20
json
21
} from "@remix-run/node";
22
```
23
24
For CommonJS:
25
26
```javascript
27
const {
28
createFileSessionStorage,
29
unstable_createFileUploadHandler,
30
installGlobals,
31
writeReadableStreamToWritable,
32
createRequestHandler,
33
json
34
} = require("@remix-run/node");
35
```
36
37
## Basic Usage
38
39
```typescript
40
import { installGlobals, createFileSessionStorage, json } from "@remix-run/node";
41
42
// Install web standard globals in Node.js
43
installGlobals();
44
45
// Create file-based session storage
46
const sessionStorage = createFileSessionStorage({
47
dir: "./sessions",
48
cookie: {
49
name: "__session",
50
maxAge: 86400,
51
httpOnly: true,
52
secure: process.env.NODE_ENV === "production"
53
}
54
});
55
56
// Use in a Remix route
57
export async function loader({ request }: LoaderFunctionArgs) {
58
const session = await sessionStorage.getSession(
59
request.headers.get("Cookie")
60
);
61
62
return json({
63
user: session.get("user")
64
});
65
}
66
```
67
68
## Architecture
69
70
@remix-run/node is built around several key components:
71
72
- **Global Polyfills**: Web standard APIs (fetch, Request, Response) for Node.js environments
73
- **Session Storage**: File-based session persistence with automatic cleanup of expired sessions
74
- **Upload Handling**: File upload processing with disk storage and size limits
75
- **Stream Utilities**: Bridges between Node.js streams and Web Streams API
76
- **Server Runtime**: Complete re-export of @remix-run/server-runtime for unified API access
77
78
## Capabilities
79
80
### Global Polyfills
81
82
Install web standard global APIs in Node.js environments using either undici or @remix-run/web-fetch.
83
84
```typescript { .api }
85
function installGlobals(options?: { nativeFetch?: boolean }): void;
86
```
87
88
[Global Polyfills](./global-polyfills.md)
89
90
### Session Storage
91
92
File-based session storage that persists session data to the filesystem with automatic expiration handling.
93
94
```typescript { .api }
95
function createFileSessionStorage<Data = SessionData, FlashData = Data>(
96
options: FileSessionStorageOptions
97
): SessionStorage<Data, FlashData>;
98
99
interface FileSessionStorageOptions {
100
cookie?: SessionIdStorageStrategy["cookie"];
101
dir: string;
102
}
103
```
104
105
[Session Storage](./session-storage.md)
106
107
### File Upload Handling
108
109
Upload handler for processing multipart form data and saving files to disk with configurable options.
110
111
```typescript { .api }
112
function unstable_createFileUploadHandler(
113
options?: FileUploadHandlerOptions
114
): UploadHandler;
115
116
class NodeOnDiskFile implements Omit<File, "constructor"> {
117
name: string;
118
size: number;
119
type: string;
120
constructor(filepath: string, type: string);
121
slice(start?: number, end?: number, type?: string): Blob;
122
arrayBuffer(): Promise<ArrayBuffer>;
123
stream(): ReadableStream;
124
text(): Promise<string>;
125
remove(): Promise<void>;
126
getFilePath(): string;
127
}
128
```
129
130
[Upload Handling](./upload-handling.md)
131
132
### Stream Utilities
133
134
Utilities for converting between Node.js streams and Web Streams API, enabling seamless data flow between different stream types.
135
136
```typescript { .api }
137
function writeReadableStreamToWritable(
138
stream: ReadableStream,
139
writable: Writable
140
): Promise<void>;
141
142
function createReadableStreamFromReadable(
143
source: Readable
144
): ReadableStream;
145
146
function readableStreamToString(
147
stream: ReadableStream<Uint8Array>,
148
encoding?: BufferEncoding
149
): Promise<string>;
150
151
function writeAsyncIterableToWritable(
152
iterable: AsyncIterable<Uint8Array>,
153
writable: Writable
154
): Promise<void>;
155
```
156
157
[Stream Utilities](./stream-utilities.md)
158
159
### Cookie and Session Implementations
160
161
Node.js-specific implementations of cookie and session utilities using cookie-signature for secure signing.
162
163
```typescript { .api }
164
function createCookie(name: string, cookieOptions?: CookieOptions): Cookie;
165
166
function createCookieSessionStorage<Data = SessionData, FlashData = Data>(
167
options: CookieSessionStorageOptions
168
): SessionStorage<Data, FlashData>;
169
170
function createSessionStorage<Data = SessionData, FlashData = Data>(
171
options: SessionStorageOptions<Data, FlashData>
172
): SessionStorage<Data, FlashData>;
173
174
function createMemorySessionStorage<Data = SessionData, FlashData = Data>(
175
options?: MemorySessionStorageOptions
176
): SessionStorage<Data, FlashData>;
177
```
178
179
[Cookie and Session Implementations](./cookie-session.md)
180
181
### Server Runtime API
182
183
Complete re-export of @remix-run/server-runtime providing unified access to all Remix server functionality including request handling, response utilities, and data fetching.
184
185
```typescript { .api }
186
function createRequestHandler(build: ServerBuild): RequestHandler;
187
188
function json<T>(object: T, init?: ResponseInit): TypedResponse<T>;
189
190
function defer<T>(object: T, init?: ResponseInit): TypedDeferredData<T>;
191
192
function redirect(url: string, init?: number | ResponseInit): Response;
193
```
194
195
[Server Runtime API](./server-runtime.md)