How To Move & Copy Files In NodeJS (Simple Examples)

Welcome to a tutorial on how to move and copy files in NodeJS. Need to back up some files in NodeJS? Move some old files to another folder?

  • To copy a file in NodeJS – require("fs").copyFileSync("SOURCE", "TARGET")
  • To move a file in NodeJS – require("fs").renameSync("SOURCE", "TARGET")

That covers the quick basics, but read on if you need more examples!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TLDR – QUICK SLIDES

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download all the example source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

 

MOVE & COPY FILES IN NODEJS

All right, let us now get into the examples of how to move and copy files in NodeJS.

 

1) COPY FILES

1-copy.js
// (A) FILE SYSTEM MODULE
const fs = require("fs"),
      source = "./README.txt",
      target = "./COPY.txt";

// (B) COPY SYNC
try {
  fs.copyFileSync(source, target);
  console.log(`Copied ${source} to ${target}`);
} catch (err) {
  console.log("Error copying file");
}

// (C) COPY ASYNC
fs.copyFile(source, target, err => {
  if (err) { console.log(err); }
  else { console.log(`Copied ${source} to ${target}`); }
});

This should be self-explanatory, we use copyFile() or copyFileSync() to copy files. Simple.

 

2) MOVE FILES

2-move.js
// (A) FILE SYSTEM MODULE
const fs = require("fs"),
      source = "./COPY.txt",
      target = "./demoA/COPY.txt";

// (B) MOVE SYNC
try {
  fs.renameSync(source, target);
  console.log(`Moved ${source} to ${target}`);
} catch (err) {
  console.log("Error copying file");
}

// (C) MOVE ASYNC
fs.rename(source, target, err => {
  if (err) { console.log(err); }
  else { console.log(`Moved ${source} to ${target}`); }
});

Yep, there’s no such thing as moveFile() in Node. We simply rename() the file to move it.

 

 

3) RECURSIVE COPY THE ENTIRE FOLDER

3-copy-all.js
// (A) FS-EXTRA MODULE
// npm install fs-extra
// https://www.npmjs.com/package/fs-extra
const fse = require("fs-extra");

// (B) COPY
try {
  fse.copySync("./demoA/", "./demoB/");
  console.log("COPY OK");
} catch (err) {
  console.log(err);
}

NodeJS does not have a native “copy all” function. So to copy an entire folder recursively, the easiest way is to use a module such as fs-extrafse.copySync("SOURCE", "DESTINATION") and that’s it.

P.S. To move the entire folder, there is a corresponding moveSync().

 

4) COPY FILES WITH EXTENSION

4-copy-ext.js
// (A) LOAD MODULES
const path = require("path"),
      fs = require("fs");

// (B) SOURCE & TARGET FOLDERS
const source = path.join(__dirname, "demoA/"),
      target = path.join(__dirname, "demoB/"),
      ext = ".txt";

// (C) LOOP THROUGH FILES
fs.readdirSync(source).forEach((file) => {
  if (path.extname(file).toLowerCase() == ext) {
    fs.copyFileSync(source + file, target + file);
    console.log(`Copied ${source + file} to ${target + file}`);
  }
});

To copy files of a certain extension – A common way is to loop through all the files, check the file extension, then only copy those that match.

 

 

EXTRA BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

LINKS & REFERENCES

 

INFOGRAPHIC CHEAT SHEET

Copy Move Files In NodeJS (Click To Enlarge)

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published.