node.js fs.exists() will be deprecated, what to use instead?

后端 未结 4 823
我在风中等你
我在风中等你 2021-02-18 13:05

According to the documentation node.js fs.exists() will be deprecated. Their reasoning:

fs.exists() is an anachronism and exists only for historical reasons.

4条回答
  •  Happy的楠姐
    2021-02-18 13:57

    Here is an example using fs.stat, but handling errors correctly as well:

    async function fileExists(filename) {
        try {
            await fs.promises.stat(filename);
            return true;
        } catch (err) {
            if (err.code === 'ENOENT') {
                return false;
            } else {
                throw err;
            }
        }
    }
    

提交回复
热议问题