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

NameTypeDescription
pathstring | Buffer | URLFile path.
flagsstring | numberOpening mode; defaults to r if omitted.
modeintegerPermissions 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(); }
Node.js Reference