Home  >  Article  >  Web Front-end  >  How to copy folders recursively in nodejs

How to copy folders recursively in nodejs

PHPz
PHPzOriginal
2023-05-14 09:03:071721browse

In Node.js, recursively copying folders is a common operation. This article will explore how to copy an entire folder, including all subfiles and subfolders within it, using Node.js.

There are two main ways to copy a folder in Node.js. One is to use the built-in fs module, and the other is to use a third-party module.

Use the built-in fs module for folder copy

The built-in fs module of Node.js provides several APIs to help us implement the folder copy function.

First, we need to create a function that will receive two parameters: the path to the source folder and the path to the destination folder.

const fs = require('fs');
const path = require('path');

function copyFolderRecursiveSync(source, target) {
  // ...
}

Next, we need to create the target folder. If the target folder does not exist, we can create it using the fs.mkdirSync() function.

if (!fs.existsSync(target)) {
  fs.mkdirSync(target);
}

Next, we need to list all files and subfolders in the source folder. We can use the fs.readdirSync() function to get the names of all files and subfolders in the source folder.

const files = fs.readdirSync(source);

For each file or subfolder, we need to check whether it is a file or a folder. We can use the fs.statSync() function to get the status of a file or folder.

files.forEach((file) => {
  const sourcePath = path.join(source, file);
  const targetPath = path.join(target, file);

  const stat = fs.statSync(sourcePath);
  if (stat.isFile()) {
    // copy file
  } else if (stat.isDirectory()) {
    // copy directory
  }

If the file is a file, we can use the fs.copyFileSync() function to copy it to the target folder.

if (stat.isFile()) {
  fs.copyFileSync(sourcePath, targetPath);
}

If the file is a folder, we need to call the copyFolderRecursiveSync() function recursively and create a new folder in the target folder.

if (stat.isDirectory()) {
  copyFolderRecursiveSync(sourcePath, targetPath);
}

The complete code is as follows:

const fs = require('fs');
const path = require('path');

function copyFolderRecursiveSync(source, target) {
  if (!fs.existsSync(target)) {
    fs.mkdirSync(target);
  }

  const files = fs.readdirSync(source);

  files.forEach((file) => {
    const sourcePath = path.join(source, file);
    const targetPath = path.join(target, file);

    const stat = fs.statSync(sourcePath);
    if (stat.isFile()) {
      fs.copyFileSync(sourcePath, targetPath);
    } else if (stat.isDirectory()) {
      copyFolderRecursiveSync(sourcePath, targetPath);
    }
  });
}

copyFolderRecursiveSync('path/to/sourceFolder', 'path/to/targetFolder');

Using third-party modules for folder copy

In addition to using the built-in fs module, you can also use third-party modules to implement folders Copy function. Here we will introduce two commonly used modules: fs-extra and ncp.

fs-extra module

fs-extra is a third-party module for Node.js that provides more functions than the built-in fs module, including copying and moving files and folders and delete.

We can use fs-extra's copySync() function to copy the entire folder, including subfolders and files within it.

const fs = require('fs-extra');

fs.copySync('path/to/sourceFolder', 'path/to/targetFolder');

ncp module

ncp is a third-party module for Node.js that can also be used to copy folders recursively. Unlike fs-extra, ncp can handle errors during replication more easily.

We can use ncp's ncp() function to copy the entire folder.

const ncp = require('ncp').ncp;

ncp('path/to/sourceFolder', 'path/to/targetFolder', function (err) {
  if (err) {
    console.error(err);
  } else {
    console.log('done');
  }
});

Summary

There are multiple ways to copy folders recursively in Node.js. Using the built-in fs module requires writing more code, but we have more control over the copying process. Folder copying is easier to achieve using the third-party modules fs-extra and ncp, but this may reduce our control over the copying process.

The above is the detailed content of How to copy folders recursively in nodejs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn