'How to create a png file with fs on heroku hosting

I'm building an api and in one of mine commands, my application needs to create a png file using fs. I am trying it by this way:

Error: ENOENT: no such file or directory, open './pictures/b1hyT.png'

The pictures folder is created, but there's any file inside of it.

Here is my code:

var download = function(uri, filename, callback){
    request.head(uri, function(err, res, body){
        console.log('content-type:', res.headers['content-type']);
        console.log('content-length:', res.headers['content-length']);

        request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
    });
};

var imageDir = `./pictures/${generatedImageId}.png`

download(`https://ui-avatars.com/api/?name=NAME&background=random&size=128`, imageDir, function(){


Solution 1:[1]

I do not really know if this would work but I had the same error with my electron app.

Try Replacing

var imageDir = `./pictures/${generatedImageId}.png`

with

var imageDir = `${__dirname}/pictures/${generatedImageId}.png`

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Retro