
Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag storeThis is a classic environment variable issue during the Gatsby build process. The error means that process.env.SANITY_PROJECT_ID is undefined when Gatsby runs your build, even though it works fine in development.
Gatsby has specific requirements for environment variables. In Gatsby, environment variables need to be:
GATSBY_ to be available in browser/build code.env filesYour current setup uses process.env.SANITY_PROJECT_ID, but Gatsby isn't making this available during the build process.
Option 1: Use Gatsby-prefixed environment variables (Recommended)
Rename your environment variables in your .env file:
GATSBY_SANITY_PROJECT_ID=your-project-id GATSBY_SANITY_DATASET=your-dataset
Then update your client configuration:
const client = require("@sanity/client");
export const sanityClient = client({
projectId: process.env.GATSBY_SANITY_PROJECT_ID,
dataset: process.env.GATSBY_SANITY_DATASET,
useCdn: false,
});Option 2: Use dotenv with Gatsby's config
If you want to keep your variable names, you can explicitly load them in gatsby-config.js:
require("dotenv").config({
path: `.env.${process.env.NODE_ENV}`,
});
module.exports = {
// your gatsby config
};Then make sure you're accessing the variables correctly and they're defined in your .env file at the root of your project.
.env file location - It should be at the root of your Gatsby project.env files - Add them to .gitignoreGATSBY_ prefixNote: You have useCdn: false which is fine for builds, but consider setting useCdn: true for production client-side requests to get better performance through Sanity's CDN.
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.
Content operations
Content backend


The only platform powering content operations
By Industry


Tecovas strengthens their customer connections
Build and Share

Grab your gear: The official Sanity swag store
Read Grab your gear: The official Sanity swag store