脚手架开发工具——判断文件是否存在 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
相关推荐
共享家95276 小时前
搭建 AI 聊天机器人:”我的人生我做主“
前端·javascript·css·python·pycharm·html·状态模式
Halo_tjn7 小时前
基于封装的专项 知识点
java·前端·python·算法
m0_7482299910 小时前
Vue2 vs Vue3:核心差异全解析
前端·javascript·vue.js
C澒10 小时前
前端监控系统的最佳实践
前端·安全·运维开发
xiaoxue..10 小时前
React 手写实现的 KeepAlive 组件
前端·javascript·react.js·面试
hhy_smile10 小时前
Class in Python
java·前端·python
小邓吖11 小时前
自己做了一个工具网站
前端·分布式·后端·中间件·架构·golang
南风知我意95711 小时前
【前端面试2】基础面试(杂项)
前端·面试·职场和发展
LJianK111 小时前
BUG: Uncaught Error: [DecimalError] Invalid argument: .0
前端
No Silver Bullet11 小时前
Nginx 内存不足对Web 应用的影响分析
运维·前端·nginx