mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
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 <vsemozhetbyt@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
1 parent
f5d42532a3
commit
d7db306d3b
1 file changed
+27
-4
+27
-4
@@ -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`);
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
|
||||
Reference in new issue
Block a user