node:fs
NodeJS
概述
对文件进行操作,包括读取、写入、删除、重命名等操作。
node:fs
模块从 v20
,其表现已足够覆盖大多数文件操作的需求,无需再依赖其他的文件系统模块。
其它可代替方案
读取文件
同步阻塞读取
import { readFileSync } from 'node:fs'
const data = readFileSync('path/to/file', 'utf8')
异步读取
import { readFile } from 'node:fs/promises'
const data = await readFile('path/to/file', 'utf8')
读取目录下所有文件
import { readdir, readFile } from 'node:fs/promises'
// `recursive: true` 递归读取
const files = await readdir('path/to/directory', { recursive: true })
const data = {}
for (const file of files) {
data[file] = await readFile(file, 'utf8')
}
写入文件
同步阻塞写入
import { writeFileSync } from 'node:fs'
writeFileSync('path/to/file', 'hello world', 'utf8')
异步写入
import { writeFile } from 'node:fs/promises'
await writeFile('path/to/file', 'hello world', 'utf8')
写入检查
import { mkdir, writeFile } from 'node:fs/promises'
import path from 'node:path'
// 确保目录存在
// recursive: true 递归创建
await mkdir(path.dirname('path/to/file'), { recursive: true })
await writeFile('path/to/file', 'hello world', 'utf8')
检查文件是否存在
import { existsSync } from 'node:fs'
if (existsSync('path/to/file')) {
// do something
}
注意
在对文件进行读写时,并不需要使用 existsSync
检查文件是否存在,而应该直接读写,通过 try-catch
捕获错误。
读写前检查文件是否存在通常被认为是一种 反模式 。
删除文件
import { unlink } from 'node:fs/promises'
await unlink('path/to/file')
删除目录
import { rm } from 'node:fs/promises'
// `recursive: true` 递归删除
await rm('path/to/directory', { recursive: true })
复制文件
import { copyFile } from 'node:fs/promises'
await copyFile('source/file', 'destination/file')
复制目录
import { cp } from 'node:fs/promises'
// `recursive: true` 递归复制
await cp('source/directory', 'destination/directory', { recursive: true })