question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Method toFile not overwriting

See original GitHub issue

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');

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
lovellcommented, May 17, 2018

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.

0reactions
ericoSoarescommented, May 17, 2018

Worked, thank you very much!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Write to text file without overwriting in Java - Stack Overflow
I am trying to write a method that makes a "log. txt file" if one does not already exist and then writes to...
Read more >
How to Overwrite a File in Python? (5 Best Methods with Code)
Learn how to overwrite a file in python with code. This includes methods like os.remove(), seek() and truncate(),replace() and more.
Read more >
file_put_contents - Manual - PHP
If filename does not exist, the file is created. Otherwise, the existing file is overwritten, unless the FILE_APPEND flag is set.
Read more >
Out-File (Microsoft.PowerShell.Utility)
This example prevents an existing file from being overwritten. By default, Out-File overwrites existing files. ... The Get-Process cmdlet gets the list of ......
Read more >
File.WriteAllText() does not overwrite? - Google Groups
I'm expecting that every time I run the dumpToFile() method it overwrites the file that already exists. Well, apparently in my case it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found