检查nodejs路径是否存在

6 javascript node.js

我只想知道路径是否存在.这是我的代码:

var path = require('path'); // exists path
Run Code Online (Sandbox Code Playgroud)

Yaş*_*ÇLİ 11

尝试:

var path = require('path');
if (path.existsSync("/the/path")) { // or fs.existsSync
    // ...
}
Run Code Online (Sandbox Code Playgroud)

要么

fs.exists('/the/path', function (exists) {
    util.debug(exists ? "it's there" : "no exists");
});
Run Code Online (Sandbox Code Playgroud)

  • `path.exists()`在0.8.0及更高版本中被弃用.请改用`fs.exists()`. (10认同)
  • `fs.exists()`也将[弃用](http://nodejs.org/api/fs.html#fs_fs_exists_path_callback).您可能希望使用某些版本的[stat](http://nodejs.org/api/fs.html#fs_fs_stat_path_callback). (5认同)