JS

HOW TO MOVE & COPY FILES IN NODEJS

const fs = require("fs");  SYNC COPY FILE try {  fs.copyFileSync(SOURCE, TARGET); } catch (err) { console.log(err); }

BASIC COPY FILE

01

ASYNC COPY FILE fs.copyFile(SOURCE, TARGET, err => {   if (err) { console.log(err); }   else { console.log("ok"); }  });

const fs = require("fs"); SYNC MOVE try { fs.renameSync(SOURCE, TARGET); } catch (err) { console.log(err); }

BASIC MOVE FILE

02

ASYNC MOVE fs.rename(SOURCE, TARGET, err => {   if (err) { console.log(err); }   else { console.log("ok"); }  });

RUN “NPM INSTALL FS-EXTRA” IN TERMINAL FIRST const fse = require("fs-extra"); COPY ALL try   { fse.copySync(SOURCE, TARGET); } catch (err) { console.log(err); } MOVE ALL try   { fse.moveSync(SOURCE, TARGET); } catch (err) { console.log(err); }

COPY MOVE ALL FILES

03