Skip to content

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 a File or Blob to 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 File objects

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 -- openLazyFile returns a LazyFile whose content is not read from disk until you call stream(), arrayBuffer(), or text(). This keeps memory usage low when serving files.
  • MIME detection -- The file's type property is automatically set based on the file extension using the mime package.
  • Auto-mkdir -- writeFile creates parent directories if they do not exist, so you never need to manually create folder structures.
  • lazy-file -- The LazyFile class returned by openLazyFile
  • mime -- MIME type detection used for the type property
  • response -- Serve files as HTTP responses
  • API Reference -- Full API documentation

Released under the MIT License.