Build issues with Gatsby and Sanity blog starter project

12 replies
Last updated: Apr 28, 2020
Hey all! Has anyone had any build issues when trying to run a gatsby build on the Sanity blog starter project? I am getting this…
AI Update

I can see you're running into build issues with the Gatsby blog starter, though your error message got cut off! Without seeing the complete error, I'll walk you through the most common build problems people encounter with Gatsby + Sanity projects.

Common Build Issues & Solutions:

1. Configuration Problems

First, verify your gatsby-config.js has the correct settings for the gatsby-source-sanity plugin:

{
  resolve: 'gatsby-source-sanity',
  options: {
    projectId: 'your-project-id',
    dataset: 'production',
    // Add a token if using overlayDrafts or private datasets
    token: process.env.SANITY_READ_TOKEN,
    watchMode: true, // for development
  }
}

2. Missing Read Token

If your dataset is private or you're using overlayDrafts: true for draft previews, you need a read token. Create one in your project's API settings at sanity.io/manage, then add it as an environment variable. Without this token, the build will fail when trying to fetch content.

3. GraphQL API Deployment (Optional)

While not strictly required for gatsby-source-sanity to work, deploying the GraphQL API can help with schema introspection. The plugin actually fetches content using Sanity's export endpoint, but having the GraphQL schema deployed helps Gatsby understand your content structure better.

If you want to deploy it, run from your Sanity studio folder:

sanity graphql deploy

4. Node Version Mismatch

Modern Sanity and Gatsby versions require Node.js 20+. Check your Node version with node -v and upgrade if needed.

5. Plugin Installation

Make sure you've installed the source plugin:

npm install gatsby-source-sanity

What Would Really Help:

If you could share the full error message you're seeing, the community can give you a much more targeted solution! Look for output after running gatsby build or gatsby develop. Common error patterns include:

  • Authentication/token errors
  • "Cannot query field..." GraphQL errors
  • Module not found errors
  • Network/connection timeouts

The exact error message will point us to the specific issue. Also helpful: which starter template are you using (blog, portfolio, etc.) and whether you've made any modifications to the default configuration?

Show original thread
12 replies
Thanks for the heads up, User! There should be a line at the top of that file:
const {format} = require('date-fns')
It should also check for empty
publishedAt
fields in the GraphQL query. I’ll check if I can reproduce the issue 👍
Thanks User. Yes I see it. I do have a draft that’s not been published and without a date. That’s been the only change on the project so far..
The graphql does filter them…

filter: { slug: { current: { ne: null } }, publishedAt: { ne: null } }
I had the same kind of problem with date in Gatsby before: that was a matter of date format. Here I see
YYYY/MM
and Gatsby only accept
YYYY-MM
or may be complete
YYYY-MM-DD
. I am not sure though.
Here is a data dump of the data it’s looping through.
Hi again User, I just tried to replicate the issue by starting from scratch at https://www.sanity.io/create with the Gatsby blog template. However, it successfully runs
gatsby develop
and
gatsby build && gatsby serve
in /web, as well as
npm run build
in root. So it looks like we need to dive in deeper to figure out what the issue is.
You created your project via
sanity.io/create , correct? And you get the error when running your build locally? Have you tried re-creating the blog from scratch?
That’s correct yeah. Haven’t tried re-creating it from scratch… i’ll give it a try tomorrow and see how I get on and let you know. Thanks for looking into it User 👍
Ok, so I can confirm that the problem was upgrading the projects dependencies. (forgot that I did that, sorry). So it appears to be a problem with 1 or more of the following deps. (Not including latest gatsby as it still builds on the below version)
Might be worth trying to replicate with that and see if you have the same build problem. I’m unable to go through each dependancie at the moment to see which ones are causing the problems, but if you’d like me to when i’m able, please let me know 🙂
There does seem to be a particular issue around using the latest date-fns, for which
user S
gave a possible work around. with the below

import { format, parseISO } from 'date-fns'
...
const dateSegment = format(parseISO(publishedAt), 'yyyy/MM')
Hi again User, thanks for confirming and for sharing all these extra details 👍 Looks like we have to do some maintenance to bring the template up to the latest version of
date-fns
without breaking it 🙂 Hope you can move forward for now, but I’ll ping you when we release an update.
Thanks User, that would be great. Thanks for your help on this 👍

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?