Check synchronously if file/directory exists in Node.js

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

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

15条回答
  •  感情败类
    2020-11-22 13:08

    Looking at the source, there's a synchronous version of path.exists - path.existsSync. Looks like it got missed in the docs.

    Update:

    path.exists and path.existsSync are now deprecated. Please use fs.exists and fs.existsSync.

    Update 2016:

    fs.exists and fs.existsSync have also been deprecated. Use fs.stat() or fs.access() instead.

    Update 2019:

    use fs.existsSync. It's not deprecated. https://nodejs.org/api/fs.html#fs_fs_existssync_path

提交回复
热议问题