diff --git a/doc/api/fs.md b/doc/api/fs.md index 3e5ff52986163a..e0ea18eb11c12c 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1307,6 +1307,35 @@ platform-specific. On macOS, Linux, and Windows, the promise will be rejected with an error. On FreeBSD, a representation of the directory's contents will be returned. +An example of reading a `package.json` file located in the same directory of the +running code: + +```mjs +import { readFile } from 'node:fs/promises'; +try { + const filePath = new URL('./package.json', import.meta.url); + const contents = await readFile(filePath, { encoding: 'utf8' }); + console.log(contents); +} catch (err) { + console.error(err.message); +} +``` + +```cjs +const { readFile } = require('node:fs/promises'); +const { resolve } = require('node:path'); +async function logFile() { + try { + const filePath = resolve('./package.json'); + const contents = await readFile(filePath, { encoding: 'utf8' }); + console.log(contents); + } catch (err) { + console.error(err.message); + } +} +logFile(); +``` + It is possible to abort an ongoing `readFile` using an {AbortSignal}. If a request is aborted the promise returned is rejected with an `AbortError`: