Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ce3cb29

Browse filesBrowse files
bnbdanielleadams
authored andcommitted
doc: add fsPromises.readFile() example
Signed-off-by: Tierney Cyren <hello@bnb.im> PR-URL: #40237 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 97df9b8 commit ce3cb29
Copy full SHA for ce3cb29

File tree

Expand file treeCollapse file tree

1 file changed

+29
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+29
-0
lines changed
Open diff view settings
Collapse file

‎doc/api/fs.md‎

Copy file name to clipboardExpand all lines: doc/api/fs.md
+29Lines changed: 29 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,35 @@ platform-specific. On macOS, Linux, and Windows, the promise will be rejected
13071307
with an error. On FreeBSD, a representation of the directory's contents will be
13081308
returned.
13091309
1310+
An example of reading a `package.json` file located in the same directory of the
1311+
running code:
1312+
1313+
```mjs
1314+
import { readFile } from 'node:fs/promises';
1315+
try {
1316+
const filePath = new URL('./package.json', import.meta.url);
1317+
const contents = await readFile(filePath, { encoding: 'utf8' });
1318+
console.log(contents);
1319+
} catch (err) {
1320+
console.error(err.message);
1321+
}
1322+
```
1323+
1324+
```cjs
1325+
const { readFile } = require('node:fs/promises');
1326+
const { resolve } = require('node:path');
1327+
async function logFile() {
1328+
try {
1329+
const filePath = resolve('./package.json');
1330+
const contents = await readFile(filePath, { encoding: 'utf8' });
1331+
console.log(contents);
1332+
} catch (err) {
1333+
console.error(err.message);
1334+
}
1335+
}
1336+
logFile();
1337+
```
1338+
13101339
It is possible to abort an ongoing `readFile` using an {AbortSignal}. If a
13111340
request is aborted the promise returned is rejected with an `AbortError`:
13121341

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.