diff --git a/doc/api/fs.md b/doc/api/fs.md index 6e3628c8fbb2a0..320f6c796e5a26 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -760,12 +760,35 @@ no effect on Windows (will behave like `fs.constants.F_OK`). The final argument, `callback`, is a callback function that is invoked with a possible error argument. If any of the accessibility checks fail, the error -argument will be an `Error` object. The following example checks if the file -`/etc/passwd` can be read and written by the current process. +argument will be an `Error` object. The following examples check if +`package.json` exists, and if it is readable or writable. ```js -fs.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK, (err) => { - console.log(err ? 'no access!' : 'can read/write'); +const file = 'package.json'; + +// Check if the file exists in the current directory. +fs.access(file, fs.constants.F_OK, (err) => { + console.log(`${file} ${err ? 'does not exist' : 'exists'}`); +}); + +// Check if the file is readable. +fs.access(file, fs.constants.R_OK, (err) => { + console.log(`${file} ${err ? 'is not readable' : 'is readable'}`); +}); + +// Check if the file is writable. +fs.access(file, fs.constants.W_OK, (err) => { + console.log(`${file} ${err ? 'is not writable' : 'is writable'}`); +}); + +// Check if the file exists in the current directory, and if it is writable. +fs.access(file, fs.constants.F_OK | fs.constants.W_OK, (err) => { + if (err) { + console.error( + `${file} ${err.code === 'ENOENT' ? 'does not exist' : 'is read-only'}`); + } else { + console.log(`${file} exists, and it is writable`); + } }); ```