ReferenceError: document is not defined during Sanity GraphQL deploy

16 replies
Last updated: Feb 5, 2021
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
  Uncaught [ReferenceError: document is not defined] at reportException (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/helpers/runtime-script-errors.js:66:24)
    at innerInvokeEventListeners (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:312:9)
    at invokeEventListeners (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:251:3)
    at DocumentImpl._dispatch (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:205:11)
    at DocumentImpl.dispatchEvent (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
    at dispatchEvent (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:488:13)
    at /Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:493:11
    at new Promise (<anonymous>)
    at onDOMContentLoad (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:491:14)
    at Object.check (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/browser/resources/resource-queue.js:76:23) ReferenceError: document is not defined
    at Object.highlightAll (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/prismjs/components/prism-core.js:530:23)
    at Document.highlightAutomaticallyCallback (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/prismjs/components/prism-core.js:1193:5)
    at innerInvokeEventListeners (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:296:27)
    at invokeEventListeners (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:251:3)
    at DocumentImpl._dispatch (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:205:11)
    at DocumentImpl.dispatchEvent (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/events/EventTarget-impl.js:87:17)
    at dispatchEvent (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:488:13)
    at /Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:493:11
    at new Promise (<anonymous>)
    at onDOMContentLoad (/Users/r.derolez/code/helloastro/helloastro-cms/node_modules/jsdom/lib/jsdom/living/nodes/Document-impl.js:491:14)
I've been getting this constantly ever since upgrading sanity this week. After incrementally adding bits to a brand new empty studio I found mine throwing this error when I included a few specific plugins.
The plugins themselves were still working and the gql redeployed correctly.

I upgraded an old build and started getting the same error. Very odd
Do you have any syntax highlighting plugin or code preview/input installed in your studio? It seems to be stemming from prism (a syntax highlighter) using
document
in a node.js context.
user Z
the only thing I've got installed is the sanity color input!
user Z
from a completely blank studio mine was producing this error when adding the "color-list" plug in. Removing the plugin stopped the error
Hm, looks like we’ve got a common denominator then!
user G
Are you also using the "color-list" plugin, or a different one? There's several color plugins out there
I’ve got the same problem, and the same plugin.
That's good to know. I'll have a look and see if I can find the culprit 🕵️‍♂️ 🔍
Found the culprit - https://github.com/sanity-io/sanity/pull/2279 Hoping to have a fix in a day or two
🙂
user Z
no I'm using
"@sanity/color-input": "^2.1.4",
actually
Awesome to see it fixed so quickly though, nice work ✌️
Fixed in v2.3.3
🤘
Nice one thanks
user Z
Cheers!! 😁

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?