perhaps? 2010-12-19 11:19 采纳率: 100%
浏览 150
已采纳

同步检查 Node.js 中是否存在文件 / 目录

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

转载于:https://stackoverflow.com/questions/4482686/check-synchronously-if-file-directory-exists-in-node-js

  • 写回答

13条回答 默认 最新

  • ?yb? 2010-12-19 11:26
    关注

    The answer to this question has changed over the years. The current answer is here at the top, followed by the various answers over the years in chronological order:

    Current Answer

    You can use fs.existsSync():

    var fs = require('fs');
    if (fs.existsSync(path)) {
        // Do something
    }
    

    It was deprecated for several years, but no longer is. From the docs:

    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.)

    You've specifically asked for a synchronous check, but if you can use an asynchronous check instead (usually best with I/O), use fs.access (since exists is deprecated).


    Historical Answers

    Here are the historical answers in chronological order:

    • Original answer from 2010
      (stat/statSync or lstat/lstatSync)
    • Update September 2012
      (exists/existsSync)
    • Update February 2015
      (Noting impending deprecation of exists/existsSync, so we're probably back to stat/statSync or lstat/lstatSync)
    • Update December 2015
      (There's also fs.access(path, fs.F_OK, function(){}) / fs.accessSync(path, fs.F_OK), but note that if the file/directory doesn't exist, it's an error; docs for fs.stat recommend using fs.access if you need to check for existence without opening)
    • Update December 2016
      fs.exists() is still deprecated but fs.existsSync() is no longer deprecated. So you can safely use it now.

    Original answer from 2010:

    You can use statSync or lstatSync (docs link), which give you an fs.Stats object. In general, if a synchronous version of a function is available, it will have the same name as the async version with Sync at the end. So statSync is the synchronous version of stat; lstatSync is the synchronous version of lstat, etc.

    lstatSync tells you both whether something exists, and if so, whether it's a file or a directory (or in some file systems, a symbolic link, block device, character device, etc.), e.g. if you need to know if it exists and is a directory:

    var fs = require('fs');
    try {
        // Query the entry
        stats = fs.lstatSync('/the/path');
    
        // Is it a directory?
        if (stats.isDirectory()) {
            // Yes it is
        }
    }
    catch (e) {
        // ...
    }
    

    ...and similarly if it's a file, there's isFile; if it's a block device, there's isBlockDevice, etc., etc. Note the try/catch; it throws an error if the entry doesn't exist at all.

    If you don't care what the entry is and only want to know whether it exists, you can use path.existsSync (or with latest, fs.existsSync) as noted by user618408:

    var path = require('path');
    if (path.existsSync("/the/path")) { // or fs.existsSync
        // ...
    }
    

    It doesn't require a try/catch, but gives you no information about what the thing is, just that it's there. path.existsSync was deprecated long ago.


    Side note: You've expressly asked how to check synchronously, so I've used the xyzSync versions of the functions above. But wherever possible, with I/O, it really is best to avoid synchronous calls. Calls into the I/O subsystem take significant time from a CPU's point of view. Note how easy it is to call lstat rather than lstatSync:

    // Is it a directory?
    lstat('/the/path', function(err, stats) {
        if (!err && stats.isDirectory()) {
            // Yes it is
        }
    });
    

    But if you need the synchronous version, it's there.

    Update September 2012

    The below answer from a couple of years ago is now a bit out of date. The current way is to use fs.existsSync to do a synchronous check for file/directory existence (or of course fs.exists for an asynchronous check), rather than the path versions below.

    Example:

    var fs = require('fs');
    
    if (fs.existsSync(path)) {
        // Do something
    }
    
    // Or
    
    fs.exists(path, function(exists) {
        if (exists) {
            // Do something
        }
    });
    

    Update February 2015

    And here we are in 2015 and the Node docs now say that fs.existsSync (and fs.exists) "will be deprecated". (Because the Node folks think it's dumb to check whether something exists before opening it, which it is; but that's not the only reason for checking whether something exists!)

    So we're probably back to the various stat methods... Until/unless this changes yet again, of course.

    Update December 2015

    Don't know how long it's been there, but there's also fs.access(path, fs.F_OK, ...) / fs.accessSync(path, fs.F_OK). And at least as of October 2016, the fs.stat documentation recommends using fs.access to do existence checks ("To check if a file exists without manipulating it afterwards, fs.access() is recommended."). But note that the access not being available is considered an error, so this would probably be best if you're expecting the file to be accessible:

    var fs = require('fs');
    
    try {
        fs.accessSync(path, fs.F_OK);
        // Do something
    } catch (e) {
        // It isn't accessible
    }
    
    // Or
    
    fs.access(path, fs.F_OK, function(err) {
        if (!err) {
            // Do something
        } else {
            // It isn't accessible
        }
    });
    

    Update December 2016

    You can use fs.existsSync():

    if (fs.existsSync(path)) {
        // Do something
    }
    

    It was deprecated for several years, but no longer is. From the docs:

    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.)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP