One thing that worked for me:
The default memory allocation for the Gatsby build process is 1.5GB. However, this default allocation may not be sufficient for larger or more complex projects, and you may need to increase the memory allocation to avoid build failures.
So I increased the memory to 3 gb by
set NODE_OPTIONS=–max_old_space_size=3072
and then running
npm start