17

I'm trying to copy an image from a folder to another using fs-extra module .

var fse = require('fs-extra');

function copyimage() {
  fse.copy('mainisp.jpg', './test', function (err) {     
    if (err) 
      return console.error(err)
  });
}

This is my directory

and this is the error I get all the time:

Error {errno: -4058, code: "ENOENT", syscall: "lstat", path: "E:\mainisp.jpg", message: "ENOENT: no such file or directory, lstat 'E:\mainisp.jpg'"}

and by changing destination to ./test/ I get this error

Error {errno: -4058, code: "ENOENT", syscall: "lstat", path: "E:\Development\Node apps\Node softwares\Digital_library\mainisp.jpg", message: "ENOENT: no such file or directory, lstat 'E:\Devel… apps\Node softwares\Digital_library\mainisp.jpg'"}

Note: I'm not testing this in browser. It's an Nwjs app and the pics of error attached are from Nwjs console.

peteb
  • 18,552
  • 9
  • 50
  • 62
Hammas
  • 1,126
  • 1
  • 12
  • 37

3 Answers3

21

You can do this using the native fs module easily using streams.

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

let filename = 'mainisp.jpg';
let src = path.join(__dirname, filename);
let destDir = path.join(__dirname, 'test');

fs.access(destDir, (err) => {
  if(err)
    fs.mkdirSync(destDir);

  copyFile(src, path.join(destDir, filename));
});


function copyFile(src, dest) {

  let readStream = fs.createReadStream(src);

  readStream.once('error', (err) => {
    console.log(err);
  });

  readStream.once('end', () => {
    console.log('done copying');
  });

  readStream.pipe(fs.createWriteStream(dest));
}
peteb
  • 18,552
  • 9
  • 50
  • 62
  • it doesn't work ! ' .. i copied and pasted your code in my function and still getting 100% second error i have mentioned a above . – Hammas Jul 26 '16 at 17:07
  • This code does work, you're just experiencing an error because the destination directory doesn't exist. At least thats what your error you've provided said. The directory path that you'd like doesn't get created along with the file. The containing folder still needs to exist prior to the file being copied. – peteb Jul 26 '16 at 17:09
  • @RaoHammasHussain added directory check before starting. – peteb Jul 26 '16 at 17:14
  • please look at the image of directory i have attached with question. The destination folder /test exists already ! – Hammas Jul 26 '16 at 17:14
  • @RaoHammasHussain this code will 100% work for that directory setup. – peteb Jul 26 '16 at 17:17
  • believe me bro ! i just tested your updated code and 100% same error 4058 ! :( my directory is same as shown in pic but still error ! – Hammas Jul 26 '16 at 17:20
  • @RaoHammasHussain have you tried running this with plain Node.js? – peteb Jul 26 '16 at 17:22
  • well no ! i am going to try this now ! i will tell you the result . – Hammas Jul 26 '16 at 17:26
  • tested ! gives syntex error ! SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode – Hammas Jul 26 '16 at 17:47
  • You're running an older version of node then, just convert the syntax to use var instead and get rid of the arrow functions. – peteb Jul 26 '16 at 17:48
  • OMG ! finally i got it :) thanks a lot bro.. but actually .. the problem was the source and destination path should be : Src => E:/Development/Node apps/Node softwares/Digital_library/html/js/test/mainisp.jpg and Dest => E:/Development/Node apps/Node softwares/Digital_library/html/js/mainisp.jpg........... it requires full address for both src and dest ! i am accepting your answer but please edit it so that others can get benefit of it :) and maye you can upvote my question :D – Hammas Jul 26 '16 at 18:22
  • @RaoHammasHussain updated my answer to include more generic pathing – peteb Jul 26 '16 at 18:33
  • 1
    I have used this way and it works but it destroys the timestamp data, is there a way to keep the original creation time and modifications time of the files? – Kamel Labiad Aug 24 '19 at 22:16
14

Try:

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

fs.copySync(path.resolve(__dirname,'./mainisp.jpg'), './test/mainisp.jpg');

As you can see in the error message, you're trying to read the file from E:\mainisp.jpg instead of the current directory.

You also need to specify the target path with the file, not only the destination folder.

alejandromav
  • 933
  • 1
  • 11
  • 24
  • thanks for the quick reply ! but the src file is in the same dir of javascript file so path could be only the name as we use it in Html or css . and i am not getting this thing "target path with the file " ! – Hammas Jul 26 '16 at 16:57
  • can you please show the exact path with above code that i should use . Image of directory is attached ! please – Hammas Jul 26 '16 at 16:58
  • @RaoHammasHussain the target path must be `./test/mainisp.jpg` isntead of `./test`. You need to specicfy the file, not just the directory. – alejandromav Jul 26 '16 at 16:59
  • and this __dirname ??? what should come here ? getting this error in nodejs test ReferenceError: path is not defined ! – Hammas Jul 26 '16 at 17:50
5

Try:

const fs = require('fs');
fs.copyFileSync(src, dest);
Karan Vyas
  • 129
  • 1
  • 4