Copy a source file to another destination in Nodejs
Asked Answered
D

3

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.

Dissatisfaction answered 26/7, 2016 at 16:42 Comment(0)
B
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.

Bleeding answered 26/7, 2016 at 16:51 Comment(4)
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 " !Dissatisfaction
can you please show the exact path with above code that i should use . Image of directory is attached ! pleaseDissatisfaction
@RaoHammasHussain the target path must be ./test/mainisp.jpg isntead of ./test. You need to specicfy the file, not just the directory.Bleeding
and this __dirname ??? what should come here ? getting this error in nodejs test ReferenceError: path is not defined !Dissatisfaction
C
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));
}
Carminacarminative answered 26/7, 2016 at 16:55 Comment(13)
it doesn't work ! ' .. i copied and pasted your code in my function and still getting 100% second error i have mentioned a above .Dissatisfaction
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.Carminacarminative
@RaoHammasHussain added directory check before starting.Carminacarminative
please look at the image of directory i have attached with question. The destination folder /test exists already !Dissatisfaction
@RaoHammasHussain this code will 100% work for that directory setup.Carminacarminative
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 !Dissatisfaction
@RaoHammasHussain have you tried running this with plain Node.js?Carminacarminative
well no ! i am going to try this now ! i will tell you the result .Dissatisfaction
tested ! gives syntex error ! SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict modeDissatisfaction
You're running an older version of node then, just convert the syntax to use var instead and get rid of the arrow functions.Carminacarminative
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 :DDissatisfaction
@RaoHammasHussain updated my answer to include more generic pathingCarminacarminative
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?Oakie
B
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.

Bleeding answered 26/7, 2016 at 16:51 Comment(4)
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 " !Dissatisfaction
can you please show the exact path with above code that i should use . Image of directory is attached ! pleaseDissatisfaction
@RaoHammasHussain the target path must be ./test/mainisp.jpg isntead of ./test. You need to specicfy the file, not just the directory.Bleeding
and this __dirname ??? what should come here ? getting this error in nodejs test ReferenceError: path is not defined !Dissatisfaction
F
5

Try:

const fs = require('fs');
fs.copyFileSync(src, dest);
Fumed answered 19/11, 2018 at 7:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.