Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ENOENT using fs.appendFile()

Tags:

node.js

fs

I am trying to append data into some files.

Docs says fs.appendFile:

Asynchronously append data to a file, creating the file if it not yet exists, data can be a string or a buffer

function appendData(products) {
var productsPromises = products.map(function(product) {
    var title = product['title'];
    return fs.appendFile('/XXXXX/' + title, product, 'utf8', function(err){
        console.log(err);
    });
});
return Promise.all(productsPromises);
}

I am getting Error:

ENOENT, open '/XXXXX/PPPPPPPP'

What am i doing wrong?

like image 231
Mimetix Avatar asked Feb 13 '23 06:02

Mimetix


2 Answers

You might have accidently added / in front of XXXXX.

I you expect it to write to a folder XXXXX which is located in the same place of where you launched the application, then change your code to:

return fs.appendFile('XXXXX/' + title, product, 'utf8', function(err){

As / In front means root of your filesystem, and the error is common of path does not exist. I.e. there is no XXXXX in root of your filesystem, as @Rahil Wazir said.

like image 81
alandarev Avatar answered Feb 15 '23 12:02

alandarev


The problem was that i forgot to add the dot.

Should be:

return fs.appendFile('./XXXXX/' + title, product, 'utf8', function(err){
like image 43
Mimetix Avatar answered Feb 15 '23 12:02

Mimetix