Node.js check if file exists

前端 未结 17 926
生来不讨喜
生来不讨喜 2020-12-07 10:02

How do i check the existence of a file?

In the documentation for the module fs there\'s a description of the method fs.exists(path, cal

17条回答
  •  半阙折子戏
    2020-12-07 10:39

    A easier way to do this synchronously.

    if (fs.existsSync('/etc/file')) {
        console.log('Found file');
    }
    

    The API doc says how existsSync work:
    Test whether or not the given path exists by checking with the file system.

提交回复
热议问题