ReferenceError: document is not defined during Sanity GraphQL deploy
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:
- Custom input components or preview components in your schema that use
document,window, or other browser APIs - Conditional logic that checks for browser globals without proper guards
- 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:
- Run with debug logging to see more details:
sanity graphql deploy --playground --debugOr set the environment variable:
SANITY_LOG_LEVEL=debug sanity graphql deployLook through your schema files (especially custom input components, preview components, and any imported utilities) for references to
document,window,navigator, or other browser APIsCheck 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 thread16 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.