Skip to content

Commit d7db306

Browse files
BeniCheniMylesBorins
authored andcommitted
doc: update examples for fs.access()
PR-URL: #20460 Fixes: #17508 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent f5d4253 commit d7db306

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

doc/api/fs.md

+27-4
Original file line numberDiff line numberDiff line change
@@ -760,12 +760,35 @@ no effect on Windows (will behave like `fs.constants.F_OK`).
760760

761761
The final argument, `callback`, is a callback function that is invoked with
762762
a possible error argument. If any of the accessibility checks fail, the error
763-
argument will be an `Error` object. The following example checks if the file
764-
`/etc/passwd` can be read and written by the current process.
763+
argument will be an `Error` object. The following examples check if
764+
`package.json` exists, and if it is readable or writable.
765765

766766
```js
767-
fs.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK, (err) => {
768-
console.log(err ? 'no access!' : 'can read/write');
767+
const file = 'package.json';
768+
769+
// Check if the file exists in the current directory.
770+
fs.access(file, fs.constants.F_OK, (err) => {
771+
console.log(`${file} ${err ? 'does not exist' : 'exists'}`);
772+
});
773+
774+
// Check if the file is readable.
775+
fs.access(file, fs.constants.R_OK, (err) => {
776+
console.log(`${file} ${err ? 'is not readable' : 'is readable'}`);
777+
});
778+
779+
// Check if the file is writable.
780+
fs.access(file, fs.constants.W_OK, (err) => {
781+
console.log(`${file} ${err ? 'is not writable' : 'is writable'}`);
782+
});
783+
784+
// Check if the file exists in the current directory, and if it is writable.
785+
fs.access(file, fs.constants.F_OK | fs.constants.W_OK, (err) => {
786+
if (err) {
787+
console.error(
788+
`${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`);
789+
} else {
790+
console.log(`${file} exists, and it is writable`);
791+
}
769792
});
770793
```
771794

0 commit comments

Comments
 (0)