Issue with creating default schema for content import in a separate repo, resolved by changing code on line 7.
I can see you're running into the "Schema.compile is not a function" error when trying to import content. This is a common issue that happens when using the older @sanity/schema package API, which has changed significantly between Sanity Studio versions.
The Schema.compile method was part of the v2 API. Here's how to fix it depending on your situation:
For Modern Sanity (v3/v4)
If you're on Studio v3 or v4, you should use the modern schema definition approach. Instead of compiling schemas manually with @sanity/schema, schemas are now defined using defineType and defineField:
import {defineType, defineField} from 'sanity'
export const mySchemaType = defineType({
name: 'myDocument',
type: 'document',
fields: [
defineField({
name: 'title',
type: 'string'
})
]
})For content imports/migrations, use the modern CLI migration tool:
npx sanity@latest migration createThis gives you a migration script where you can use defineMigration without needing to manually compile schemas.
If You Need Schema.compile (Legacy v2 or Portable Text conversion)
If you're specifically working with Portable Text conversion or maintaining v2 code, the correct import syntax is:
import Schema from '@sanity/schema'
const defaultSchema = Schema.compile({
name: 'mySchema',
types: [
// your schema types here
]
})The key issue is the import syntax. You need to import the default export from @sanity/schema, not a named export:
✅ Correct: import Schema from '@sanity/schema'
❌ Wrong: import { Schema } from '@sanity/schema'
The Portable Text documentation shows this pattern for HTML-to-Portable-Text conversion:
import Schema from '@sanity/schema'
import blockTools from '@portabletext/block-tools'
const defaultSchema = Schema.compile({
name: 'default',
types: schemaTypes
})
const blockContentType = defaultSchema.get('blogPost')
.fields.find(field => field.name === 'body').type
const blocks = blockTools.htmlToBlocks(
'<html><body><h1>Hello world!</h1></body></html>',
blockContentType
)Common Issues Checklist
- Wrong import syntax: Make sure you're using
import Schema from '@sanity/schema'(default import) - Version mismatch: If you're on Studio v3+, you shouldn't need
@sanity/schemafor most use cases - Missing package: Ensure
@sanity/schemais installed:npm install @sanity/schema
If you're migrating content to Sanity, I'd strongly recommend using the modern migration tools (npx sanity migration create) rather than manually compiling schemas. This approach is better supported, has better error handling, and includes features like dry-run mode and validation.
Could you share what version of Sanity Studio you're using and what you're trying to accomplish with the import? That would help me give you more specific guidance!
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.