Absolute file paths - targeting public dir


My website requests a script within .netlify/functions/myfunctiondir/functionname.js which attempts to save the results to the public space, but I’m unsuccessfully targeting it.

const dir = path.resolve(__dirname, '../../../public/savedresults');
if (!fs.existsSync(dir)) fs.mkdirSync(dir);

It results in a 502 error:
"Error: ENOENT: no such file or directory, mkdir '/var/task/public/savedresults'"

Any tips/advice?

Hi @silentfail :wave:t2:

Unfortunately that’s not possible. Architecturally, you really have to consider your Function folder to be truly standalone - that folder gets zipped into its own package then shipped off to AWS lambda where it will run on a machine somewhere with zero context of where it came from, that it’s tied to Netlify in any way, or what previous runs of the function did. A site’s static build and its functions are fully disconnected in terms of systems / machines / file structures / etc. – your functions are running fully stateless somewhere in the AWS cloud and your site files themselves are being served directly from Netlify’s Edge Network on CDN-like servers (geo-distributed). The two actually don’t know about each other at all. A Function cannot impact the site’s live files directly.

That said, there are some folks experimenting with Functions that commit to the underlying repository as a means of programmatically changing the site. GoTell is one such example - creating a comment system based on a repository containing a JSON file that a Function updates when a new comment comes in, commits the change, then a Netlify build kicks off. The transaction time is significantly slower than a normal web request (on the order of minutes // based on your build time rather than milliseconds), but it the use case works for you, it could be really useful.

I hope that helps! Happy to answer any followup questions


Thanks for the detailed reply, @jonsully. What you say makes sense; I guess I’m confused by posts like this (so I assume he’s running and saving images on AWS, not that the Lambda runs on AWS and then works with the file setup on Netlify)? https://gersom.nl/post/og-images-on-netlify/

I didn’t read through that whole post (just have a few minutes here currently) but that blog post is using a build step of Gatsby’s build process, not a fully separate Function that exists and runs whenever called. You’re totally free to add custom build scripts to Netlify, and the files you play with and save there would be used in the resulting site. Does that make sense? The build phase of the static site is a fully different process from the compile-and-ship phase of the Functions


I think what you’re describing are build plugins (regarding Netlify), which are a new thing, so will have to read up on how to create my own to do what I want. Thanks again for the feedback.

No problem!

I should clarify though - build plugins are another separate concept. Build plugins do run post-site built but are a second, additional (optional) step after your site’s own build command. I believe the article you referenced executed its steps as part of the site’s own build. You could do this as a build plugin, but you could also just make it part of the build itself. I know there’s a few layers there and that may be tough to feel out but I think it’s important to make the delineation :slight_smile:


Hi, @silentfail, Netlify functions are deployed on AWS Lambda and, as such, there is no persistent filesystem to write to.

If there are other questions about this, please let us know.