脚手架开发工具——判断文件是否存在 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
相关推荐
曦樂~3 小时前
【Web Development】02 CSS
前端·css
风骏时光牛马3 小时前
后端服务接口开发与数据逻辑处理
前端
东鹏特饮3 小时前
经典打砖块(Breakout)游戏
前端·css·css3
IT_陈寒4 小时前
Vite的HMR把我整不会了:一个生产环境的热更新坑
前端·人工智能·后端
Mh4 小时前
如何使用双缓冲优化视频切换
前端
risenav4 小时前
低头蓄力,抬头起跳:《萤虫跳跳》的 Rokid AIUI 开发实战
前端
ZhengEnCi4 小时前
如何看待 Fable 5.2 用纯代码生成 1.4MB 视觉作品:光追、水面、倒影、火光的程序化生成原理
前端
再吃一根胡萝卜5 小时前
DSH 插件的 Host 与 Client:为什么「只加载了 Host」,以及要不要自己写 Client 加载器
前端
parade岁月5 小时前
AI 写的前端代码为什么总像 5 年前的?Chrome 官方出手了
前端
tryCbest6 小时前
Vue系列——第 15 篇:内置组件全家桶——Transition、Teleport、Suspense、KeepAlive
前端·vue.js