脚手架开发工具——判断文件是否存在 path-exists

简介

path-exists 是一个轻量级的 Node.js npm 包,其核心作用是简便、高效地检查文件系统中指定的路径(文件或目录)是否存在,无需开发者手动封装原生文件操作的回调逻辑或错误处理,简化了 Node.js 中的路径存在性校验场景。

核心用法

lua 复制代码
npm install path-exists --save
  • 异步用法(推荐,非阻塞 I/O)
js 复制代码
const pathExists = require('path-exists');

// 异步检查文件路径是否存在
async function checkFilePath() {
  // 传入要检查的文件/目录路径(相对路径或绝对路径均可)
  const isExist = await pathExists('./test.txt');
  console.log('文件是否存在:', isExist); // 返回 true 或 false
}

checkFilePath();

// 也可使用 Promise 链式调用(兼容旧版语法)
pathExists('./dist/').then((exists) => {
  console.log('目录是否存在:', exists);
});
  • 同步用法(适用于简单脚本,阻塞 I/O)
ini 复制代码
const pathExists = require('path-exists');

// 同步检查目录路径是否存在
const dirExists = pathExists.sync('./node_modules/');
console.log('node_modules 目录是否存在:', dirExists); // 返回 true 或 false
相关推荐
代码搬运媛1 小时前
Jest 测试框架详解与实现指南
前端
counterxing2 小时前
我把 Codex 里的 Skills 做成了一个 MCP,还支持分享
前端·agent·ai编程
wangqiaowq2 小时前
windows下nginx的安装
linux·服务器·前端
之歆2 小时前
DAY_12JavaScript DOM 完全指南(二):实战与性能篇
开发语言·前端·javascript·ecmascript
发现一只大呆瓜2 小时前
Vite凭什么这么快?3分钟带你彻底搞懂 Vite 热更新的幕后黑手
前端·面试·vite
Maimai108083 小时前
React如何用 @microsoft/fetch-event-source 落地 SSE:比原生 EventSource 更灵活的实时推送方案
前端·javascript·react.js·microsoft·前端框架·reactjs·webassembly
kyriewen4 小时前
产品经理把PRD写成“天书”,我用AI半小时重写了一遍,他当场愣住
前端·ai编程·cursor
humcomm5 小时前
元框架的工作原理详解
前端·前端框架
canonical_entropy5 小时前
Attractor Before Harness: AI 大规模开发的方法论
前端·aigc·ai编程
zhangxingchao5 小时前
多 Agent 架构到底怎么选?从 Claude Agent Teams、Cognition/Devin 到工程落地原则
前端·人工智能·后端