Check synchronously if file/directory exists in Node.js

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

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

15条回答
  •  被撕碎了的回忆
    2020-11-22 13:02

    The path module does not provide a synchronous version of path.exists so you have to trick around with the fs module.

    Fastest thing I can imagine is using fs.realpathSync which will throw an error that you have to catch, so you need to make your own wrapper function with a try/catch.

提交回复
热议问题