From b5d8353dc76666baa2c36940726c9cb90394bc7c Mon Sep 17 00:00:00 2001 From: Christopn Noelke Date: Sat, 17 Sep 2016 11:03:04 +0200 Subject: [PATCH 1/3] 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 --- doc/api/fs.md | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/doc/api/fs.md b/doc/api/fs.md index 2b9369ffa3b04c..45265f38839dcf 100644 --- a/doc/api/fs.md +++ b/doc/api/fs.md @@ -1517,6 +1517,27 @@ 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][]. + +For example: + +**check for file existence** + +```js +fs.stat('myfile', (err, stats) => { + if (err) { + if (err.code === 'ENOENT') { + console.error('myfile does not exist.'); + return; + } else { + throw err; + } + } else { + // work with the file + } +}); +``` + ## fs.statSync(path)