So, I am downloading an image to the server and then using Sharp to resize it. So in the end I expect the following:
image.png
imageResized.png
Both should be the same image but different width and height.
Problem is, the first time I run the program, it works as expected, if I run it again without restarting the server, the "image.png" changes but "imageResized.png" remains the same. It's as if it couldn't overwrite it.
I've also tried to give the "imageResized.png" file different names for each time its generated, like this:
image.png (original image, which is generated each time the program is run)
imageResized.png (resized image, generated during the first execution)
imageResized2.png (second execution)
imageResized3.png (third execution)
In this case, "imageResized.png", "imageResized2.png" and "imageResized3.png" are the same when they should be each one a unique image.
I think this is related to maybe some cached data since I am getting no errors from the code.
I've tried to put the "sharp.cache({ files: 0 });" at the start of the file, but it didn't work.
The code I am using to resize the images is:
sharp('/src/assets/bar.png').resize(600).toFile('/src/assets/bar2.png');
"getting no errors from the code"
Hello, I don't see any error handling in the example code. Are you able to provide more complete, reproducible code that matches the description of the problem along with the input image?
I am facing the same issue. Even this simple example is not working. File gets 'touched' but seems sharp keeps referencing the old file
let width = 100;
let height = 100;
let file = './original.jpg'
sharp(file)
.resize(width, height)
.toFile(file.replace(".jpg",".lowres.jpg"))
.then((data)=>{
// foo
})
.catch(err=>{
// bar
})
this is inside an event listened by chokidar, so subsequent calls (every time a file gets added / updated on a folder) should trigger it. Using node-thumbnail (included code for reference) works fine so it's not a chokidar issue I think
```
thumb({
source: file,
destination: path.dirname(file),
suffix: '.lowres',
width: width,
height: height,
overwrite: true,
})
.then(()=>{
})
.catch(err=>{
})
Same problem even with toBuffer
.toBuffer()
.then((data)=>{
return fs.writeFileSync(file.replace(".jpg",".lowres.jpg"), data);
})
```
First call works fine, subsequent calls fail. I can see the thumb "flash" in OSX Finder, the timestamp gets updated but the thumbnail, even if you change the source file, will be the same old thumbnail.
All the relevant code I am using is this:
let uriBar = req.query.bar;
var extBar = uriBar.split(';')[0].match(/jpeg|png|gif/)[0];
var dataBar = uriBar.replace(/^data:image\/\w+;base64,/, "");
var bufBar = new Buffer(dataBar, 'base64');
fs.writeFile('/src/assets/bar.' + extBar, bufBar, () => {
sharp('/src/assets/bar.png')
.resize(600)
.toFile('/src/assets/bar2.png');
});
This is triggered when and endpoint of the API is called.
The input image is in .png format and is generated from a canvas (as a base64 string)

Overall it is the same problem as the guy above described.
Just to clarify, inside the than and catch I have error / message logging. The catch never gets hit. The thumbnail is correctly generated (hence the updated timestamp) it's just overwritten but with the old resource, not the new one.
Yes, exactly, I've also verified that the .then() is getting 'touched' and a new file is being generated each time but it's always the same as the first one, I suppose it's due to some cache'd data or something.
Thanks both, if the input filename remains the same but its contents change then adding sharp.cache(false); to your respective examples should deal with this.
In addition, if chokidar is used then ensure its awaitWriteFinish setting is true.
Thank you! Yes awaitWriteFinish is true, I think I tried setting cache to false but maybe I used sharp.cache( { files: 0 } );. I'll try passing false as an argument as you suggest and inform you if it fixes so you can close the issue.
Edit: Solved. Thanks!
Worked, thank you very much!
Most helpful comment
Thanks both, if the input filename remains the same but its contents change then adding
sharp.cache(false);to your respective examples should deal with this.In addition, if
chokidaris used then ensure itsawaitWriteFinishsetting istrue.