脚手架开发工具——判断文件是否存在 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
相关推荐
code_YuJun2 小时前
脚手架开发工具——root-check
前端
用户54277848515402 小时前
XMLHttpRequest、AJAX、Fetch 与 Axios
前端
打小就很皮...2 小时前
React 实现富文本(使用篇&Next.js)
前端·react.js·富文本·next.js
智算菩萨3 小时前
实战:高级中文自然语言处理系统的Python设计与实现
前端·javascript·easyui
远山无期3 小时前
解决Tailwind任意值滥用:规范化CSS开发体验
前端·css·eslint
用户54277848515403 小时前
Vue 3 中开发高阶组件(HOC)与 Renderless 组件
前端
HIT_Weston3 小时前
67、【Ubuntu】【Hugo】搭建私人博客(一)
前端·ubuntu·hugo
阿里巴啦4 小时前
用React+Three.js 做 3D Web版搭建三维交互场景:模型的可视化摆放与轻量交互
前端·react·three.js·模型可视化·web三维·web三维交互场景
Liu.7744 小时前
vue3组件之间传输数据
前端·javascript·vue.js