Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d7db306

Browse filesBrowse files
BeniCheniMylesBorins
authored andcommitted
doc: update examples for fs.access()
PR-URL: #20460 Fixes: #17508 Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent f5d4253 commit d7db306
Copy full SHA for d7db306

File tree

Expand file treeCollapse file tree

1 file changed

+27
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

1 file changed

+27
-4
lines changed
Open diff view settings
Collapse file

‎doc/api/fs.md‎

Copy file name to clipboardExpand all lines: doc/api/fs.md
+27-4Lines changed: 27 additions & 4 deletions
  • Display the source diff
  • Display the rich diff
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.