The fact that your app seems to get all/most of the way through but then fails when it comes time to build might mean that the build command you need for your ssg and the build command you have in your netlify build settings might be mismatched, does that make sense?
I’d look through that post, and double check your settings, and see if that helps. Good luck.