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!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

Source code on GitHub Gist

Just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

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 = "./SOURCE.txt",
      target = "./TARGET.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 = "./SOURCE.txt",
      target = "./FOLDER/TARGET.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("./SOURCE/", "./TARGET/");
  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, "SOURCE/"),
      target = path.join(__dirname, "TARGET/"),
      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.

 

 

EXTRAS

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

 

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 *