Copy folder recursively in Node.js

前端 未结 25 2026
隐瞒了意图╮
隐瞒了意图╮ 2020-12-04 07:44

Is there an easier way to copy a folder and all its content without manually doing a sequence of fs.readir, fs.readfile, fs.writefile

25条回答
  •  旧时难觅i
    2020-12-04 08:09

    If you want to copy all contents of source directory recursively then you need to pass recursive option as true and try catch is documented way by fs-extra for sync

    As fs-extra is complete replacement of fs so you don't need to import the base module

    const fs = require('fs-extra');
    let sourceDir = '/tmp/src_dir';
    let destDir = '/tmp/dest_dir';
    try {
      fs.copySync(sourceDir, destDir, { recursive: true })
      console.log('success!')
    } catch (err) {
      console.error(err)
    }
    
    

提交回复
热议问题