From b7f01ef61a95160ee3bd56af49522304fcca817a Mon Sep 17 00:00:00 2001 From: Christopn Noelke Date: Sat, 17 Sep 2016 11:03:04 +0200 Subject: [PATCH] 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: https://github.com/nodejs/node/pull/8585 Reviewed-By: James M Snell Reviewed-By: Ilkka Myller --- doc/api/fs.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index fef4516958df5b..5cb4b9f422e9fb 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -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)