Check synchronously if file/directory exists in Node.js

前端 未结 15 2357
梦如初夏
梦如初夏 2020-11-22 12:26

How can I synchronously check, using node.js, if a file or directory exists?

15条回答
  •  无人共我
    2020-11-22 13:17

    Some answers here says that fs.exists and fs.existsSync are both deprecated. According to the docs this is no more true. Only fs.exists is deprected now:

    Note that fs.exists() is deprecated, but fs.existsSync() is not. (The callback parameter to fs.exists() accepts parameters that are inconsistent with other Node.js callbacks. fs.existsSync() does not use a callback.)

    So you can safely use fs.existsSync() to synchronously check if a file exists.

提交回复
热议问题