File system
fs/promises.open()
Opens a file asynchronously and returns an owned FileHandle. Close it explicitly in finally. Use read-only flags for inspection and exclusive create where appropriate.
Formal Syntax & Structure
nodejs syntaxnodejs
open(path, flags[, mode])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| path | string | Buffer | URL | File path. |
| flags | string | number | Opening mode; defaults to r if omitted. |
| mode | integer | Permissions for newly created files, subject to umask. |
Return Value
Promise<FileHandle>
Open file handle, or rejection on error.
Working Example
Example Snippetnodejs
import { open } from "node:fs/promises";const file = await open("sample.txt", "r");try { console.log(await file.readFile("utf8")); } finally { await file.close(); }