Copy folder recursively in Node.js

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

    There are some modules that support copying folders with their content. The most popular would be wrench.js:

    // Deep-copy an existing directory
    wrench.copyDirSyncRecursive('directory_to_copy', 'location_where_copy_should_end_up');
    

    An alternative would be node-fs-extra:

    fs.copy('/tmp/mydir', '/tmp/mynewdir', function (err) {
      if (err) {
        console.error(err);
      } else {
        console.log("success!");
      }
    }); // Copies directory, even if it has subdirectories or files
    

提交回复
热议问题