Rao Hammas Hussain Rao Hammas Hussain - 1 year ago 135
Node.js Question

Copy a source file to another destination in Nodejs

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

and by changing destination to
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.

Answer Source

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) => {

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

function copyFile(src, dest) {

  let readStream = fs.createReadStream(src);

  readStream.once('error', (err) => {

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download