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.

 

 

REAL QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes Move Copy Useful Bits & Links
The End

 

DOWNLOAD & NOTES

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

 

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.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer 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.

 

 

MOVE & COPY FILES

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 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.

 

USEFUL 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.

 

INFOGRAPHIC CHEAT SHEET

Copy Move Files In NodeJS (Click To Enlarge)

 

LINKS & REFERENCES

 

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. Required fields are marked *