Access Filesystem during build (node during build?)

Hey!
So, here is the problem I am facing:

I am attempting to pre-render my site at build time with react-snap. This all works fine. However, now that I have the CMS that creates posts, I need a page of the app to PULL in all the files in a directory of the source tree so that I can then import them dynamically.

Obviously, Netlify cannot run a node server, however, during the BUILD time, I was wondering if you can use node? I have a node script that will access the filesystem and return the files back to the site.

However, I then attempted to spin this up via a function and use that function in the pre-render during the build (It worked in netlify dev) However, it turns out that functions live in a different space so apparently cannot access your built sites directory/file system.

Does anyone have an idea on how I can access my filesystem at build time to pull ALL files (unknown names) from a directory (posts). All I would need is the names of the files, not the content itself (maybe).
OR
Is there a way in NetlifyCMS to generate a JSON of all the .md posts made during build? (That would make it easier haha)

Thanks in advance.

Hi Aaron and super sorry to be slow to get back to you.

Yes - functions are run in AWS, and will not have access to your build (plus, you are running the function from some prior build (or other site), since the one being deployed is not running until around the moment the deploy completes.

You can instead run a node script during build that isn’t a function:

node script.js

  • perhaps after build:

npm run build && node script.js

Folks use this functionality to do things like “walk my deploy directory and create one redirect per directory” or other things that consider “all files in the build” programatically so maybe that’s a way to simplify & accomplish your goals?

I’m not sure that extending the NetlifyCMS is a reasonable path - it certainly won’t be easy and it feels kind of like a better solution is outside of it - but that’s just my (somewhat informed) guess.

Not sure, let me know what you think!

1 Like

Hey! This is exactly what I ended up doing, a prebuild node script to create a json of the files. I’ve not progressed much further to see if it’s viable yet since I have some interviews im preparing for :slight_smile: Thanks for helping out!