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
| Name | Type | Description |
|---|---|---|
| path | string | Buffer | URL | FileHandle | Target file or open handle. |
| options | string | object | Encoding 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"));