Copy folder recursively in Node.js

前端 未结 25 2023
隐瞒了意图╮
隐瞒了意图╮ 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条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-04 08:20

    Mallikarjun M, thank you!

    fs-extra did the thing and it can even return a Promise if you do not provide a callback! :)

    const path = require('path')
    const fs = require('fs-extra')
    
    let source = path.resolve( __dirname, 'folderA')
    let destination = path.resolve( __dirname, 'folderB')
    
    fs.copy(source, destination)
      .then(() => console.log('Copy completed!'))
      .catch( err => {
        console.log('An error occurred while copying the folder.')
        return console.error(err)
      })
    

提交回复
热议问题