Node.js操作子目录需掌握fs模块的异步API,核心方法包括使用fs.promises配合async/await实现目录的创建(mkdir,recursive: true)、读取(readdir)、删除(rm,recursive: true和force: true)及重命名(rename),路径处理应避免相对路径陷阱,优先使用__dirname和path.join确保正确性,递归遍历可通过判断dirent类型实现深度遍历,删除非空目录推荐现代API fs.rm()以简化操作并增强健壮性。

Node.js操作子目录,核心在于灵活运用内置的
fs
要使用Node.js操作子目录,我们主要依赖
fs
fs.promises
async/await
1. 创建子目录: 如果你需要创建一个多层级的子目录(比如
./a/b/c
recursive: true
import { mkdir } from 'node:fs/promises';
import { join } from 'node:path';
async function createSubdirectory(basePath, subDirName) {
const fullPath = join(basePath, subDirName);
try {
await mkdir(fullPath, { recursive: true });
console.log(`目录 '${fullPath}' 已创建。`);
} catch (err) {
if (err.code === 'EEXIST') {
console.log(`目录 '${fullPath}' 已经存在。`);
} else {
console.error(`创建目录时出错: ${err.message}`);
}
}
}
// 示例:在当前目录下创建 'data/logs/2023'
createSubdirectory(process.cwd(), 'data/logs/2023');2. 读取子目录内容: 获取一个目录下的所有文件和子目录名。
import { readdir, stat } from 'node:fs/promises';
import { join } from 'node:path';
async function listDirectoryContents(dirPath) {
try {
const entries = await readdir(dirPath, { withFileTypes: true }); // withFileTypes 很有用,可以直接判断类型
console.log(`目录 '${dirPath}' 的内容:`);
for (const entry of entries) {
if (entry.isDirectory()) {
console.log(` [目录] ${entry.name}`);
} else if (entry.isFile()) {
console.log(` [文件] ${entry.name}`);
} else {
console.log(` [其他] ${entry.name}`);
}
}
} catch (err) {
console.error(`读取目录时出错: ${err.message}`);
}
}
// 示例:列出当前目录下的内容
listDirectoryContents(process.cwd());3. 删除子目录: 删除非空子目录,务必使用
recursive: true
force: true
import { rm } from 'node:fs/promises';
import { join } from 'node:path';
async function removeSubdirectory(basePath, subDirName) {
const fullPath = join(basePath, subDirName);
try {
await rm(fullPath, { recursive: true, force: true });
console.log(`目录 '${fullPath}' 及其内容已删除。`);
} catch (err) {
console.error(`删除目录时出错: ${err.message}`);
}
}
// 示例:删除之前创建的 'data' 目录
// 注意:这会删除 'data' 及其所有子内容,请谨慎操作!
removeSubdirectory(process.cwd(), 'data');4. 重命名或移动子目录:
fs.rename
import { rename } from 'node:fs/promises';
import { join } from 'node:path';
async function renameOrMoveSubdirectory(oldPath, newPath) {
try {
await rename(oldPath, newPath);
console.log(`目录已从 '${oldPath}' 移动/重命名到 '${newPath}'。`);
} catch (err) {
console.error(`重命名/移动目录时出错: ${err.message}`);
}
}
// 示例:将 'old_dir' 重命名为 'new_dir'
// 先创建 'old_dir' 才能测试
// await createSubdirectory(process.cwd(), 'old_dir');
// renameOrMoveSubdirectory(join(process.cwd(), 'old_dir'), join(process.cwd(), 'new_dir'));
// 示例:将 'source_dir' 移动到 'destination/target_dir'
// await createSubdirectory(process.cwd(), 'source_dir');
// await createSubdirectory(process.cwd(), 'destination');
// renameOrMoveSubdirectory(join(process.cwd(), 'source_dir'), join(process.cwd(), 'destination', 'target_dir'));递归遍历子目录结构,这在处理文件上传、备份、内容索引等场景下非常常见。我的经验是,使用
async/await
fs.promises
基本思路是:
这里有一个实用的递归遍历函数示例:
import { readdir, stat } from 'node:fs/promises';
import { join } from 'node:path';
async function traverseDirectory(currentPath, indent = 0) {
const prefix = ' '.repeat(indent); // 用于美化输出的缩进
try {
const entries = await readdir(currentPath, { withFileTypes: true });
for (const entry of entries) {
const entryPath = join(currentPath, entry.name);
if (entry.isDirectory()) {
console.log(`${prefix}[目录] ${entry.name}`);
await traverseDirectory(entryPath, indent + 1); // 递归调用
} else if (entry.isFile()) {
console.log(`${prefix}[文件] ${entry.name}`);
}
// 也可以处理其他类型,比如符号链接等
}
} catch (err) {
// 权限问题 (EACCES) 或路径不存在 (ENOENT) 是常见的错误
console.error(`${prefix}遍历目录 '${currentPath}' 时出错: ${err.message}`);
}
}
// 示例:从当前目录开始递归遍历
// 建议在一个测试目录中运行,避免遍历整个项目目录导致输出过多
// 例如,先创建一个测试结构:
// mkdir -p test_dir/sub1/sub1_1
// touch test_dir/file1.txt test_dir/sub1/file2.log
// await traverseDirectory(join(process.cwd(), 'test_dir'));
// traverseDirectory(process.cwd()); // 遍历当前项目目录在实际项目中,我们可能需要对遍历的结果进行收集,而不是简单地打印。这可以通过传递一个数组或映射来收集文件路径、目录结构等信息。另外,对于非常大的文件系统,需要考虑异步操作的并发限制,避免同时打开过多文件句柄,这可以通过
p-limit
async.queue
路径问题,说实话,是我在Node.js文件系统操作中踩坑最多的地方,没有之一。它不像代码逻辑那么直观,往往是环境差异或者一些默认行为导致的问题。
1. 相对路径与绝对路径的混淆:
./
../
process.cwd()
src/utils/
./data
./data
src/utils/data
__dirname
__filename
path.join(__dirname, 'my_subdir')
path.resolve()
path.join()
path.join()
..
.
path.resolve()
/
process.cwd()
import { join, resolve } from 'node:path';
console.log(join('/foo', 'bar', 'baz/asdf', 'quux', '..')); // /foo/bar/baz/asdf
console.log(resolve('/foo/bar', './baz')); // /foo/bar/baz
console.log(resolve('/foo/bar', '/tmp/file/')); // /tmp/file
console.log(resolve('wwwroot', 'static_files/png/', '../gif/index.gif')); // C:\Users\...\wwwroot\static_files\gif\index.gif (Windows)2. 跨平台路径分隔符: Windows使用
\
/
path.join()
path.resolve()
path.sep
path.join
3. 异步操作中的路径上下文: 在复杂的异步流程中,如果路径是动态生成的或者依赖于某个异步操作的结果,一定要确保路径在文件操作执行时是正确的、完整的。有时候一个变量名写错,或者一个异步回调的参数没传对,都会导致路径错误,而且这种错误往往很难排查。
4. 权限问题: 这不是严格意义上的路径问题,但经常伴随路径问题出现。如果你尝试在一个没有写入权限的目录创建文件,或者读取一个没有读取权限的目录,Node.js会抛出
EACCES
删除包含文件的非空子目录,这听起来简单,但如果操作不当,可能会导致数据丢失或程序崩溃。在Node.js中,这块的API经历了一些演变,现在已经非常方便了。
现代Node.js (v14.0.0+): 使用 fs.rm()
从Node.js 14开始,
fs.rm()
fs.promises.rm()
fs.unlink()
fs.rmdir()
关键在于使用
recursive: true
force: true
import { rm } from 'node:fs/promises';
import { join } from 'node:path';
async function safelyRemoveDirectory(dirPath) {
try {
// recursive: true 允许删除非空目录及其所有内容
// force: true 忽略路径不存在的错误,如果目录不存在,不会报错
await rm(dirPath, { recursive: true, force: true });
console.log(`目录 '${dirPath}' 及其所有内容已安全删除。`);
} catch (err) {
// 仍然需要捕获其他可能的错误,例如权限问题
console.error(`删除目录 '${dirPath}' 时出错: ${err.message}`);
}
}
// 示例:
// 假设有一个名为 'temp_data' 的目录,里面有文件和子目录
// await safelyRemoveDirectory(join(process.cwd(), 'temp_data'));为什么recursive: true
force: true
recursive: true
fs.rm()
fs.rmdir()
ENOTEMPTY
force: true
rm
ENOENT
旧版Node.js (< v14) 或更精细的控制:手动递归删除
在
fs.rm()
fs.unlink()
fs.rmdir()
这个过程要复杂得多,涉及大量的错误处理和异步流程控制。因此,如果你的Node.js版本支持
fs.rm()
重要提示: 删除操作是不可逆的!在执行任何删除子目录的代码之前,请务必仔细检查目标路径,并确保你有正确的备份策略。尤其是在生产环境中,一个错误的路径可能导致灾难性的数据丢失。我通常会在删除操作前加一个日志,明确要删除什么,甚至在关键操作前要求人工确认。
以上就是怎样使用Node.js操作子目录?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号