Skip to content

Commit 143d38c

Browse files
dfabulichjasnell
authored andcommitted
doc: bad/better examples for fs.access() and fs.exists()
PR-URL: #7832 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent baa0ffd commit 143d38c

File tree

1 file changed

+164
-5
lines changed

1 file changed

+164
-5
lines changed

doc/api/fs.md

+164-5
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,93 @@ fs.access('/etc/passwd', fs.constants.R_OK | fs.constants.W_OK, (err) => {
347347
});
348348
```
349349

350+
Using `fs.access()` to check for the accessibility of a file before calling
351+
`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. Doing
352+
so introduces a race condition, since other processes may change the file's
353+
state between the two calls. Instead, user code should open/read/write the
354+
file directly and handle the error raised if the file is not accessible.
355+
356+
For example:
357+
358+
359+
**write (NOT RECOMMENDED)**
360+
361+
```js
362+
fs.access('myfile', (err) => {
363+
if (!err) {
364+
console.error('myfile already exists');
365+
return;
366+
}
367+
368+
fs.open('myfile', 'wx', (err, fd) => {
369+
if (err) throw err;
370+
writeMyData(fd);
371+
});
372+
});
373+
```
374+
375+
**write (RECOMMENDED)**
376+
377+
```js
378+
fs.open('myfile', 'wx', (err, fd) => {
379+
if (err) {
380+
if (err.code === "EEXIST") {
381+
console.error('myfile already exists');
382+
return;
383+
} else {
384+
throw err;
385+
}
386+
}
387+
388+
writeMyData(fd);
389+
});
390+
```
391+
392+
**read (NOT RECOMMENDED)**
393+
394+
```js
395+
fs.access('myfile', (err) => {
396+
if (err) {
397+
if (err.code === "ENOENT") {
398+
console.error('myfile does not exist');
399+
return;
400+
} else {
401+
throw err;
402+
}
403+
}
404+
405+
fs.open('myfile', 'r', (err, fd) => {
406+
if (err) throw err;
407+
readMyData(fd);
408+
});
409+
});
410+
```
411+
412+
**read (RECOMMENDED)**
413+
414+
```js
415+
fs.open('myfile', 'r', (err, fd) => {
416+
if (err) {
417+
if (err.code === "ENOENT") {
418+
console.error('myfile does not exist');
419+
return;
420+
} else {
421+
throw err;
422+
}
423+
}
424+
425+
readMyData(fd);
426+
});
427+
```
428+
429+
The "not recommended" examples above check for accessibility and then use the
430+
file; the "recommended" examples are better because they use the file directly
431+
and handle the error, if any.
432+
433+
In general, check for the accessibility of a file only if the file won’t be
434+
used directly, for example when its accessibility is a signal from another
435+
process.
436+
350437
## fs.accessSync(path[, mode])
351438
<!-- YAML
352439
added: v0.11.15
@@ -606,11 +693,83 @@ fs.exists('/etc/passwd', (exists) => {
606693
});
607694
```
608695

609-
`fs.exists()` should not be used to check if a file exists before calling
610-
`fs.open()`. Doing so introduces a race condition since other processes may
611-
change the file's state between the two calls. Instead, user code should
612-
call `fs.open()` directly and handle the error raised if the file is
613-
non-existent.
696+
Using `fs.exists()` to check for the existence of a file before calling
697+
`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended. Doing
698+
so introduces a race condition, since other processes may change the file's
699+
state between the two calls. Instead, user code should open/read/write the
700+
file directly and handle the error raised if the file does not exist.
701+
702+
For example:
703+
704+
**write (NOT RECOMMENDED)**
705+
706+
```js
707+
fs.exists('myfile', (exists) => {
708+
if (exists) {
709+
console.error('myfile already exists');
710+
} else {
711+
fs.open('myfile', 'wx', (err, fd) => {
712+
if (err) throw err;
713+
writeMyData(fd);
714+
});
715+
}
716+
});
717+
```
718+
719+
**write (RECOMMENDED)**
720+
721+
```js
722+
fs.open('myfile', 'wx', (err, fd) => {
723+
if (err) {
724+
if (err.code === "EEXIST") {
725+
console.error('myfile already exists');
726+
return;
727+
} else {
728+
throw err;
729+
}
730+
}
731+
writeMyData(fd);
732+
});
733+
```
734+
735+
**read (NOT RECOMMENDED)**
736+
737+
```js
738+
fs.exists('myfile', (exists) => {
739+
if (exists) {
740+
fs.open('myfile', 'r', (err, fd) => {
741+
readMyData(fd);
742+
});
743+
} else {
744+
console.error('myfile does not exist');
745+
}
746+
});
747+
```
748+
749+
**read (RECOMMENDED)**
750+
751+
```js
752+
fs.open('myfile', 'r', (err, fd) => {
753+
if (err) {
754+
if (err.code === "ENOENT") {
755+
console.error('myfile does not exist');
756+
return;
757+
} else {
758+
throw err;
759+
}
760+
} else {
761+
readMyData(fd);
762+
}
763+
});
764+
```
765+
766+
The "not recommended" examples above check for existence and then use the
767+
file; the "recommended" examples are better because they use the file directly
768+
and handle the error, if any.
769+
770+
In general, check for the existence of a file only if the file won’t be
771+
used directly, for example when its existence is a signal from another
772+
process.
614773

615774
## fs.existsSync(path)
616775
<!-- YAML

0 commit comments

Comments
 (0)