File system

fs.readFileSync()

Reads an entire file synchronously. An encoding returns text; otherwise a Buffer is returned. Throws on failure; avoid in latency-sensitive request handlers.

Formal Syntax & Structure

nodejs syntaxnodejs
fs.readFileSync(path[, options])

Parameters & Attributes

NameTypeDescription
pathstring | Buffer | URL | integerFile path or descriptor.
optionsstring | objectEncoding string or options object, including encoding and flag.

Return Value

string | Buffer

File contents.

Working Example

Example Snippetnodejs
import { readFileSync } from "node:fs";console.log(readFileSync("sample.txt", "utf8"));
Node.js Reference