FS Overview
The fs package bridges the gap between Node.js filesystem operations and the web-standard File API used throughout Remix. It provides two functions:
openLazyFile(path)-- Opens a file on disk and returns a LazyFile with metadata (name, type, size, lastModified) read from the filesystem. Content is deferred until accessed.writeFile(path, file)-- Writes aFileorBlobto disk, creating parent directories automatically.
When to Use This Package
Use fs when you need to:
- Serve local files as HTTP responses without loading them into memory
- Save uploaded files to disk
- Bridge between filesystem paths and web
Fileobjects
Quick Example
ts
import { openLazyFile, writeFile } from 'remix/fs'
// Read a file from disk (metadata only -- content is deferred)
let file = await openLazyFile('./uploads/photo.jpg')
console.log(file.name) // 'photo.jpg'
console.log(file.type) // 'image/jpeg'
console.log(file.size) // 2097152
console.log(file.lastModified) // 1704067200000
// Write a file to disk
let report = new File(['Q1 results'], 'report.txt', { type: 'text/plain' })
await writeFile('./reports/2025/q1.txt', report)Key Concepts
- Lazy reading --
openLazyFilereturns aLazyFilewhose content is not read from disk until you callstream(),arrayBuffer(), ortext(). This keeps memory usage low when serving files. - MIME detection -- The file's
typeproperty is automatically set based on the file extension using the mime package. - Auto-mkdir --
writeFilecreates parent directories if they do not exist, so you never need to manually create folder structures.
Related
- lazy-file -- The
LazyFileclass returned byopenLazyFile - mime -- MIME type detection used for the
typeproperty - response -- Serve files as HTTP responses
- API Reference -- Full API documentation