Skip to content

Commit

Permalink
doc: add example for file existence with fs.stat
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Christopn Noelke authored and Fishrock123 committed Oct 11, 2016
1 parent 313a45d commit 2fad4d2
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions doc/api/fs.md
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,16 @@ Asynchronous stat(2). The callback gets two arguments `(err, stats)` where
`stats` is a [`fs.Stats`][] object. See the [`fs.Stats`][] section for more
information.

In case of an error, the `err.code` will be one of [Common System Errors][].

Using `fs.stat()` to check for the existence of a file before calling
`fs.open()`, `fs.readFile()` or `fs.writeFile()` is not recommended.
Instead, user code should open/read/write the file directly and handle the
error raised if the file is not available.

To check if a file exists without manipulating it afterwards, [`fs.access()`]
is recommended.

## fs.statSync(path)
<!-- YAML
added: v0.1.21
Expand Down Expand Up @@ -2207,3 +2217,4 @@ The following constants are meant for use with the [`fs.Stats`][] object's
[`event ports`]: http://illumos.org/man/port_create
[`ReadDirectoryChangesW`]: https://msdn.microsoft.com/en-us/library/windows/desktop/aa365465%28v=vs.85%29.aspx
[`AHAFS`]: https://www.ibm.com/developerworks/aix/library/au-aix_event_infrastructure/
[Common System Errors]: errors.html#errors_common_system_errors

0 comments on commit 2fad4d2

Please sign in to comment.