Issue with deploying GraphQL endpoint for Sanity Studio

3 replies
Last updated: May 10, 2024
SOLVEDKeeping it if anybody runs into the same issue, solution in thread
---------------------------------

Hi everyone
👋
I’m writing because I’m struggling with deploying a GraphQL endpoint for my Sanity Studio.
I chose to self-host the studio in my project, so my application’s files are colocated with the ones for the studio, and the studio UI can be accessed at
/studio
from my main page.
I saw that the GraphQL endpoint was not automatically deployed when deploying the studio, and that I should run the
sanity graphql deploy
command.So I tried doing that, but I’m getting a weird error mentioning an ES modules issue:

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/[...]/node_modules/string-width/index.js from /Users/[...]/node_modules/cliui/build/index.cjs not supported.
Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules.
I checked that I’m not using any
require
statement in my code and I followed all the tutorial steps, have read a lot of the docs and the forums but didn’t find any solution to this problem. Would anyone have an idea how to unblock me please? I’m using node
v18.19.0
and you’ll find my dependencies’ details in the thread 💡
Thanks a lot for your help!
🙏
AI Update

Good news that you found a solution! For anyone else encountering this Error [ERR_REQUIRE_ESM]: require() of ES Module string-width from cliui not supported error when running sanity graphql deploy, this is typically a Node.js version compatibility issue with the Sanity CLI and its dependencies.

The error occurs because some dependencies in the Sanity CLI toolchain have moved to ES modules, which can cause conflicts with certain Node.js versions. Here are the solutions that typically work:

Primary Solution:

  1. Delete your node_modules folder and lock file (package-lock.json, yarn.lock, or pnpm-lock.yaml)
  2. Upgrade to a more recent Node.js version - Sanity Studio v4 requires Node.js 20+, and Node 21+ is recommended for avoiding these CLI issues
  3. Reinstall your dependencies with npm install (or your package manager of choice)
  4. Try sanity graphql deploy again

Important Context: As mentioned in the GraphQL deployment documentation, the GraphQL endpoint is not automatically deployed when you deploy your studio. You need to run sanity graphql deploy manually, and you'll need to re-deploy it every time you update your Sanity schema (not just content changes).

Additional Notes:

  • Make sure you're running the command from your Sanity project directory where your schema is defined
  • You can use --dry-run to validate your API before deploying
  • The GraphQL playground is publicly accessible by default; use --no-playground if you want to disable it

If upgrading Node.js doesn't resolve the issue, double-check that your package.json has "type": "module" if you're using ES modules throughout your project, or ensure consistency in how you're importing/requiring modules across your configuration files.

"dependencies": {
  "@sanity/client": "^6.12.4",
  "@sanity/icons": "^2.8",
  "@sanity/image-url": "1",
  "@sanity/types": "^3.25",
  "@sanity/ui": "^2.0",
  "@sanity/vision": "3",
  "clsx": "^2.1.0",
  "next": "^14.1",
  "next-sanity": "7.1.4",
  "react": "^18.2",
  "react-dom": "^18",
  "sanity": "^3.25",
  "styled-components": "^6.0"
}
After searching the Github forums of the faulty packages, I started meddling with the peer dependencies resolutions but it didn’t work, so I ended up deleting my
yarn.lock
and
node_modules
folder and reinstalling everything fresh with node 21, and now everything works fine 👌
Nice one mate, the fix helped me out! 🤘

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?