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
| Name | Type | Description |
|---|---|---|
| path | string | Buffer | URL | integer | File path or descriptor. |
| options | string | object | Encoding 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"));