File system

fs/promises.readFile()

Asynchronously reads an entire file; returns a promise and rejects on errors. Use a defined encoding for text and bound the file size by the application contract.

Formal Syntax & Structure

nodejs syntaxnodejs
readFile(path[, options])

Parameters & Attributes

NameTypeDescription
pathstring | Buffer | URL | FileHandleTarget file or open handle.
optionsstring | objectEncoding or options including flag and signal.

Return Value

Promise<string | Buffer>

Fulfilled with file contents.

Working Example

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