Copy folder recursively in Node.js

前端 未结 25 2038
隐瞒了意图╮
隐瞒了意图╮ 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:33

    If you are on Linux, and performance is not an issue, you may use the exec function from child_process module, to execute a Bash command:

    const { exec } = require('child_process');
    exec('cp -r source dest', (error, stdout, stderr) => {...});
    

    In some cases, I found this solution cleaner than downloading an entire module or even using the fs module.

提交回复
热议问题