Node fs copy folder

I am trying to copy a folder using the Node module fs. I am familiar with the methods readFileSync()and writeFileSync(), but I wonder what method should I use to copy the specified folder?

+4
source share
2 answers

You can use fs-extra to copy the contents of one folder to another, for example

var fs = require("fs-extra");

fs.copy('/path/to/source', '/path/to/destination', function (err) {
  if (err) return console.error(err)
  console.log('success!')
});

There is also a synchronous version.

+10
source

You might want to check out ncp . He does exactly what you are trying to do; Recursively copy files from a path to another.

Here's something to start with:

const fs = require("fs");
const path = require("path");
const ncp = require("ncp").ncp;
// No limit, because why not?
ncp.limit = 0;

var thePath = "./";
var folder = "testFolder";
var newFolder = "newTestFolder";

ncp(path.join(thePath, folder), path.join(thePath, newFolder), function (err) {
    if (err) {
        return console.error(err);
    }
    console.log("Done !");
});
0
source

Source: https://habr.com/ru/post/1652219/


All Articles