Fetching image during build

During build, I’m fetching an image from an API (using promises and 18’s fetch API), store it locally and then modify it using sharp.
Locally, everything works fine, but when running on Netlify, the build always breaks stating that it can’t find the local image url path.

I have set netlify to the same node version as my local dev.

Since netlify only shows very limited error messages, I was hoping someone could tell me what might be the deal here. Does netlify not wait for await fetch during build? Does it not allow storing files/pictures during build?

Thx for your help!

Hi @MentalGear

Where is the script you are fetching from writing the fetched image file to? And what is the path you are trying to access the image on?

10:29:34 PM: > vite build

10:29:34 PM: (node:1736) ExperimentalWarning: The Fetch API is an experimental feature. This feature could change at any time

10:29:34 PM: (Use `node --trace-warnings ...` to show where the warning was created)

10:29:35 PM: error during build:

10:29:35 PM: Error: /opt/build/repo/src/assets/dailyContent/dailyImage.jpg: unable to open for write

10:29:35 PM: unix error: No such file or directory

PS: netlify assets optimizations are deactivated.

Are you able to share the repository you are deploying from?

No, I’m afraid not. . Is there a way to inspect the build filesystem?

No there isn’t.

The filesystem is Linux, thus is case sensitive. Perhaps the directory you are trying to write to it dailycontent and not dailyContent.

Nope, all cases match.
I am using path.resolve(__dirname, filePath) … maybe __dirname is set differently on netlify’s build image. I assume /opt/build/repo/ is the normal directory netlify uses though and is not write protected.

Without seeing the code, I can’t offer much in the way of a solution.

Yeah, makes sense. Thx anyways!

Echoing @coelmay, we will need further details to look into this. If you are able to create a test reproduction, don’t hesitate to follow up here.

1 Like