Troubleshooting schema extraction error in Sanity CLI version 3.45.0

6 replies
Last updated: Jun 11, 2024
Hi all. I’m trying to extract my schema to json. I’m running the extract command but am getting the following error

$ sanity schema extract

sanity schema extractError: "extract" is not a subcommand of "sanity schema".  See "sanity --help"
    at CommandRunner.resolveSubcommand (~/.nvm/versions/node/v18.20.2/lib/node_modules/@sanity/cli/lib/_chunks-cjs/cli.js:71778:13)
    at CommandRunner.resolveCommand (~/.nvm/versions/node/v18.20.2/lib/node_modules/@sanity/cli/lib/_chunks-cjs/cli.js:71754:31)
    at CommandRunner.runCommand (~/.nvm/versions/node/v18.20.2/lib/node_modules/@sanity/cli/lib/_chunks-cjs/cli.js:71722:116)
    at Object.runCli (~/.nvm/versions/node/v18.20.2/lib/node_modules/@sanity/cli/lib/_chunks-cjs/cli.js:72111:38)
I’m running on version
3.45.0
on node 18.20.2 but I also get the same error on node 20*

$ sanity help schema

usage: sanity schema [--default] [-v|--version] [-d|--debug] [-h|--help] <command> [<args>]

Commands:
   validate  Validates all schema types specified in a workspace.

See 'sanity help schema <command>' for specific information on a subcommand.
AI Update

The sanity schema extract command is part of the TypeGen feature, which requires Sanity CLI version 3.35.0 or later according to the official documentation. You're running version 3.45.0, which should definitely have this command, so something else is going on.

The issue is almost certainly that you have a global installation of the Sanity CLI that's outdated or conflicting with your project's local version. Even though sanity --version reports 3.45.0, the global CLI binary may not be properly updated.

Solution 1: Use npx (Recommended)

Instead of using the globally installed CLI, run the command with npx to use the latest version:

npx sanity@latest schema extract

This ensures you're using the most recent CLI version regardless of what's installed globally.

Solution 2: Update Global CLI

If you prefer using the global CLI, update it:

npm install -g @sanity/cli@latest

Then verify the version:

sanity --version

Solution 3: Use Local CLI

If you have @sanity/cli installed as a project dependency, you can use it directly:

npx sanity schema extract

or add it to your package.json scripts:

{
  "scripts": {
    "extract-schema": "sanity schema extract"
  }
}

Then run npm run extract-schema.

What the command does:

The sanity schema extract command generates a schema.json file containing a static representation of your Studio schema. This is typically the first step in the TypeGen workflow, which then allows you to generate TypeScript types with sanity typegen generate.

The most common cause of this error is having an outdated global CLI installation that doesn't include the newer schema extraction feature, even though it reports a version number that should support it. Using npx sanity@latest bypasses this issue entirely and is the quickest way to get unblocked.

Is
3.45.0
the version of the Studio or the CLI that you’re running?
sorry, the cli
Hmm, it looks like it’s present for me on that version of the CLI. What’s your output from
sanity versions
? If you have the sanity package installed globally, what does
npm list -g
output for your version? Do they match?
$ sanity versions

@sanity/cli (global)                   3.45.0 (up to date)
@sanity/document-internationalization   2.0.3 (latest: 3.0.0)
@sanity/icons                          2.10.1 (latest: 3.2.0)
@sanity/image-url                       1.0.2 (up to date)
@sanity/language-filter                 3.2.2 (latest: 4.0.2)
@sanity/pkg-utils                       3.3.8 (latest: 6.9.3)
@sanity/plugin-kit                     3.1.10 (latest: 4.0.17)
@sanity/types                          3.12.0 (latest: 3.45.0)
@sanity/ui                              1.5.0 (latest: 2.3.2)
@sanity/vision                         3.28.0 (latest: 3.45.0)
sanity                                 3.28.0 (latest: 3.45.0)
Ahh so does the local package need to also be >3.35.0?


$ npm list -g               
/Users/simonlegg/.nvm/versions/node/v18.20.2/lib
├── @sanity/cli@3.45.0
...
Ok, the issue was that the
package.json
had
"sanity": "^3.14"
and I guess not having the trailing patch number meant that yarn never grabbed minor version upgrades (odd).
Seems to work now. Although as feedback, it seems weird from a user POV that I guess the cli defers internally to a locally installed version?

Either way, thank you for prompting the fix! Much appreciated
:rubberduck:
Glad you got it sorted 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?