From d7db306d3bd65485caabff3219a7eb0932bdbc19 Mon Sep 17 00:00:00 2001 From: BeniCheni Date: Tue, 1 May 2018 19:31:28 -0400 Subject: [PATCH] doc: update examples for fs.access() PR-URL: https://github.com/nodejs/node/pull/20460 Fixes: https://github.com/nodejs/node/issues/17508 Reviewed-By: Vse Mozhet Byt Reviewed-By: Trivikram Kamat --- doc/api/fs.md | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/doc/api/fs.md b/doc/api/fs.md index 644dde05004..da2b263e163 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`); + } }); ```