fs.existsSync(path)


如果路径存在,则返回 true,否则返回 false

【Returns true if the path exists, false otherwise.】

有关详细信息,请参阅此 API 异步版本的文档:fs.exists()

【For detailed information, see the documentation of the asynchronous version of this API: fs.exists().】

fs.exists() 已被弃用,但 fs.existsSync() 没有被弃用。fs.exists()callback 参数接受的参数与其他 Node.js 回调不一致。fs.existsSync() 不使用回调。

import { existsSync } from 'node:fs';

if (existsSync('/etc/passwd'))
  console.log('The path exists.');