ReferenceError: document is not defined during Sanity GraphQL deploy

16 replies
Last updated: Jan 27, 2026
Anyone ever run into this one?
Uncaught [ReferenceError: document is not defined]
when deploying the graphql? It errors midway but still successfully deploys πŸ€” (Just ran a sanity upgrade just in case, but still persists!)
Anyone ever run into this one?  `Uncaught [ReferenceError: document is not defined]`
yarn deploy:graphql
yarn run v1.22.10
$ sanity graphql deploy --playground
Dataset: production
Tag: default

βœ” Checking for deployed API
βœ” Generating GraphQL schema
β ‹ Validating GraphQL APIError: Uncaught [ReferenceError: document is not defined]
–––––[FULL ERROR IN THREAD]–––––
βœ” Validating GraphQL API
βœ” Deploying GraphQL API
GraphQL API deployed to:
[myGraphQlPlaygroundUrlHere]
AI Update

This is a known issue that occurs when your Sanity schema contains code that references browser-specific globals like document or window, which aren't available in the Node.js environment where the GraphQL deploy command runs.

The good news is that your deployment is actually succeeding despite the error! The "βœ” Deploying GraphQL API" and "GraphQL API deployed to..." messages confirm this. The error happens during the validation step but doesn't block the actual deployment.

Common causes:

  1. Custom input components or preview components in your schema that use document, window, or other browser APIs
  2. Conditional logic that checks for browser globals without proper guards
  3. Imported libraries that assume a browser environment

How to fix it:

The best approach is to wrap any browser-specific code in conditional checks:

// In your schema files
const MyComponent = () => {
  if (typeof document !== 'undefined') {
    // Browser-only code here
  }
}

Or use lazy loading for components:

components: {
  input: typeof document !== 'undefined' 
    ? require('./MyCustomInput').default 
    : undefined
}

Debugging steps:

  1. Run with debug logging to see more details:
sanity graphql deploy --playground --debug

Or set the environment variable:

SANITY_LOG_LEVEL=debug sanity graphql deploy
  1. Look through your schema files (especially custom input components, preview components, and any imported utilities) for references to document, window, navigator, or other browser APIs

  2. Check any third-party libraries you're importing in your schema - they might assume a browser environment

Since the deployment is working, you could technically leave it as-is, but fixing the root cause will clean up your deploy logs and prevent potential issues if Sanity changes how schema validation works in future versions.

If you're having trouble tracking down where the document reference is coming from, try temporarily removing custom components from your schema one by one until the error disappears - that'll help you isolate which file is causing it.

Show original thread
16 replies

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?