Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: ENOENT, no such file or directory - fs.createReadStream that is piped into a fs.createWriteStream

Tags:

file

node.js

fs

Below is my code where I am trying to use fs.createReadStream that is piped into a fs.createWriteStream. Its working for one hit. But if I run the same code using concurrent hits getting error as

Error: ENOENT, no such file or directory

var fileReadStream = fs.createReadStream("test.png", {encoding: "utf16le"});
var fileWriteStream = fs.createWriteStream("test1.png", {encoding: "utf16le"});
fileReadStream.pipe(fileWriteStream);
fileReadStream.on('end', function () {
});
fileWriteStream.on('close', function () {
    fileReadStream.on('finish', function () {
        fs.unlink(test1.png);
    })

})

I am not that good in filesystem api's. Can someone help me what I am doing wrong here.

like image 723
user4324324 Avatar asked Oct 13 '15 11:10

user4324324


People also ask

What is FS createWriteStream?

The fs. createWriteStream(path, [options]) method returns a writable stream object for file at path . The following options are available: flags : File mode argument as a string.

Does createWriteStream overwrite file?

createWriteStream() in append mode overwrites files on Windows #1897.

What is pipe in Nodejs?

pipe() method in a Readable Stream is used to attach a Writable stream to the readable stream so that it consequently switches into flowing mode and then pushes all the data that it has to the attached Writable. Syntax: readable.pipe( destination, options )


1 Answers

Add __dirname + '/test.png' that fixed my error

 var fileReadStream = fs.createReadStream(__dirname + <path to image From current directory>, {encoding: "utf16le"});
var fileWriteStream = fs.createWriteStream(__dirname + <path to image From current directory>, {encoding: "utf16le"});
like image 60
sebastian ward Avatar answered Oct 24 '22 00:10

sebastian ward