Node 本身有 fs 模块,但并不是很好用,fs-extra 是其替代品。fs-extra 所有异步API都支持 callback 和 promise 两种调用方式,使用起来很方便。

增

复制 copy

复制文件或文件夹,当复制文件夹时,dest 参数不能是目录名

  1. fs.copy('/tmp/myfile', '/tmp/mynewfile', err => {
  2. if (err) return console.error(err)
  3. console.log('success!')
  4. }) // copies file
  5. fs.copy('/tmp/mydir', '/tmp/mynewdir', err => {
  6. if (err) return console.error(err)
  7. console.log('success!')
  8. }) // copies directory, even if it has subdirectories or files

移动 move

移动文件或文件夹,当复制文件夹时,dest 参数不能是目录名

  1. fs.move(src, dest)
  2. .then(() => {
  3. console.log('success!')
  4. })
  5. .catch(err => {
  6. console.error(err)
  7. })
  8. // set overrite:true
  9. fs.move('/tmp/somedir', '/tmp/may/already/exist/somedir', { overwrite: true }, err => {
  10. if (err) return console.error(err)
  11. console.log('success!')
  12. })

生成文件 outputFile

  1. const file = '/tmp/this/path/does/not/exist/file.txt'
  2. fs.outputFile(file, 'hello!')
  3. .then(() => fs.readFile(file, 'utf8'))
  4. .then(data => {
  5. console.log(data) // => hello!
  6. })
  7. .catch(err => {
  8. console.error(err)
  9. })

生成文件 outputJson/readJson/writeJson

writeJson 和 outputJson 几乎一致,但 outputJson 如果 file 不存在会自动创建一个,更好用

  1. const file = '/tmp/this/path/does/not/exist/file.json'
  2. fs.outputJson(file, {name: 'JP'})
  3. .then(() => fs.readJson(file))
  4. .then(data => {
  5. console.log(data.name) // => JP
  6. })
  7. .catch(err => {
  8. console.error(err)
  9. })

删

删除目录 emptyDir

清空目录,若目录不存在,则创建空目录

  1. const fs = require('fs-extra')
  2. // assume this directory has a lot of files and folders
  3. // With a callback:
  4. fs.emptyDir('/tmp/some/dir', err => {
  5. if (err) return console.error(err)
  6. console.log('success!')
  7. })
  8. // With Promises:
  9. fs.emptyDir('/tmp/some/dir')
  10. .then(() => {
  11. console.log('success!')
  12. })
  13. .catch(err => {
  14. console.error(err)
  15. })
  16. // With async/await:
  17. async function example () {
  18. try {
  19. await fs.emptyDir('/tmp/some/dir')
  20. console.log('success!')
  21. } catch (err) {
  22. console.error(err)
  23. }
  24. }
  25. example()

删除文件或目录 remove

如果目录名不存在,则什么也不做,不像 emptyDir 会创建目录

  1. fs.remove('/tmp/myfile')
  2. .then(() => {
  3. console.log('success!')
  4. })
  5. .catch(err => {
  6. console.error(err)
  7. })

查

目录是否存在 ensureDir

  1. fs.ensureDir(dir)
  2. .then(() => {
  3. console.log('success!')
  4. })
  5. .catch(err => {
  6. console.error(err)
  7. })

文件是否存在 ensureFile

  1. fs.ensureFile(file)
  2. .then(() => {
  3. console.log('success!')
  4. })
  5. .catch(err => {
  6. console.error(err)
  7. })

链接(硬/软)是否存在 ensureLink/ensureSymlink

  1. const srcPath = '/tmp/file.txt'
  2. const destPath = '/tmp/this/path/does/not/exist/file.txt'
  3. fs.ensureLink(srcPath, destPath)
  4. .then(() => {
  5. console.log('success!')
  6. })
  7. .catch(err => {
  8. console.error(err)
  9. })
  10. fs.ensureSymlink(srcPath, destPath)
  11. .then(() => {
  12. console.log('success!')
  13. })
  14. .catch(err => {
  15. console.error(err)
  16. })

路径是否存在

  1. const file = '/tmp/this/path/does/not/exist/file.txt'
  2. fs.pathExists(file)
  3. .then(exists => console.log(exists)) // => false

graceful-fs

此外,fs-extra 集成了 graceful-fs,并向外暴露了 graceful-fs 的方法,也就是说 graceful-fs 的方法也是可以直接在 fs-extra 里使用的:

  1. const api = [
  2. 'access',
  3. 'appendFile',
  4. 'chmod',
  5. 'chown',
  6. 'close',
  7. 'copyFile',
  8. 'fchmod',
  9. 'fchown',
  10. 'fdatasync',
  11. 'fstat',
  12. 'fsync',
  13. 'ftruncate',
  14. 'futimes',
  15. 'lchmod',
  16. 'lchown',
  17. 'link',
  18. 'lstat',
  19. 'mkdir',
  20. 'mkdtemp',
  21. 'open',
  22. 'opendir',
  23. 'readdir',
  24. 'readFile',
  25. 'readlink',
  26. 'realpath',
  27. 'rename',
  28. 'rm',
  29. 'rmdir',
  30. 'stat',
  31. 'symlink',
  32. 'truncate',
  33. 'unlink',
  34. 'utimes',
  35. 'writeFile'
  36. ].filter(key => {
  37. // Some commands are not available on some systems. Ex:
  38. // fs.opendir was added in Node.js v12.12.0
  39. // fs.rm was added in Node.js v14.14.0
  40. // fs.lchown is not available on at least some Linux
  41. return typeof fs[key] === 'function'
  42. })