diff --git a/doc/api/fs.md b/doc/api/fs.md index 286ffb3a63f..cfa21733069 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -3443,6 +3443,7 @@ added: v10.0.0 Closes the file descriptor. ```js +const fsPromises = require('fs').promises; async function openAndClose() { let filehandle; try { @@ -3554,6 +3555,9 @@ For example, the following program retains only the first four bytes of the file: ```js +const fs = require('fs'); +const fsPromises = fs.promises; + console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js @@ -3570,6 +3574,9 @@ If the file previously was shorter than `len` bytes, it is extended, and the extended part is filled with null bytes (`'\0'`). For example, ```js +const fs = require('fs'); +const fsPromises = fs.promises; + console.log(fs.readFileSync('temp.txt', 'utf8')); // Prints: Node.js @@ -3674,6 +3681,9 @@ with an `Error` object. The following example checks if the file `/etc/passwd` can be read and written by the current process. ```js +const fs = require('fs'); +const fsPromises = fs.promises; + fsPromises.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK) .then(() => console.log('can access')) .catch(() => console.error('cannot access')); @@ -3766,7 +3776,7 @@ then the operation will fail. Example: ```js -const fs = require('fs'); +const fsPromises = require('fs').promises; // destination.txt will be created or overwritten by default. fsPromises.copyFile('source.txt', 'destination.txt') @@ -3779,6 +3789,7 @@ following example. ```js const fs = require('fs'); +const fsPromises = fs.promises; const { COPYFILE_EXCL } = fs.constants; // By using COPYFILE_EXCL, the operation will fail if destination.txt exists.