Skip to content

Commit 2fad4d2

Browse files
Christopn NoelkeFishrock123
authored andcommitted
doc: add example for file existence with fs.stat
Add an example on how to test if a file exists with fs.stat. Also add a link to the Common System Errors. Fixes: https://github.com/nodejs/issues/6752 PR-URL: #8585 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ilkka Myller <ilkka.myller@nodefield.com>
1 parent 313a45d commit 2fad4d2

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

doc/api/fs.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,16 @@ Asynchronous stat(2). The callback gets two arguments `(err, stats)` where
15241524
`stats` is a [`fs.Stats`][] object. See the [`fs.Stats`][] section for more
15251525
information.
15261526

1527+
In case of an error, the `err.code` will be one of [Common System Errors][].
1528+
1529+
Using `fs.stat()` to check for the existence of a file before calling
1530+
`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended.
1531+
Instead, user code should open/read/write the file directly and handle the
1532+
error raised if the file is not available.
1533+
1534+
To check if a file exists without manipulating it afterwards, [`fs.access()`]
1535+
is recommended.
1536+
15271537
## fs.statSync(path)
15281538
<!-- YAML
15291539
added: v0.1.21
@@ -2207,3 +2217,4 @@ The following constants are meant for use with the [`fs.Stats`][] object's
22072217
[`event ports`]: http://illumos.org/man/port_create
22082218
[`ReadDirectoryChangesW`]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v=vs.85%29.aspx
22092219
[`AHAFS`]: https://www.ibm.com/developerworks/aix/library/au-aix_event_infrastructure/
2220+
[Common System Errors]: errors.html#errors_common_system_errors

0 commit comments

Comments
 (0)