Issue with creating default schema for content import in a separate repo, resolved by changing code on line 7.

11 replies
Last updated: Feb 5, 2022
Hi Everyone!
I’m having a pretty hard time creating default schema for a content Import. Error: Schema.compile is not a function. Codes in comment and any help is appreciated!
AI Update

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 create

This 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

  1. Wrong import syntax: Make sure you're using import Schema from '@sanity/schema' (default import)
  2. Version mismatch: If you're on Studio v3+, you shouldn't need @sanity/schema for most use cases
  3. Missing package: Ensure @sanity/schema is 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!

import blockTools from '@sanity/block-tools';

import sanitizeHTML from './sanitize-html.js';

import blocksToHtml from '@sanity/block-content-to-html';

import Schema from '@sanity/schema';

import { JSDOM } from 'jsdom';


const defaultSchema = Schema.compile({

name: 'myBlog',

types: [

{

type: 'object',

name: 'post',

fields: [

{

title: 'Title',

type: 'string',

name: 'title',

},

{

title: 'Body',

name: 'body',

type: 'array',

of: [{ type: 'block' }],

},

],

},

],

});


function htmlToBlocks(html, options) {

if (!html) {

return [<bunch of rules>];

}


const blockContentType = defaultSchema

.get('post')

.fields.find((field) => field.name === 'body').type;


const blocks = blockTools.htmlToBlocks(sanitizeHTML(html), blockContentType, {

parseHtml: (htmlContent) => new JSDOM(htmlContent).window.document,

rules: [],

});

return blocks;

}


export default (bodyHTML) => htmlToBlocks(bodyHTML);
Hey Geoff, Long time no chat, Hope you are doing awesome!
I’m running it outside of any Sanity install in it’s own repo.
Now that I type that out loud I realize this may be why it’s not working.
I’m working on building a migration process using this for reference: https://github.com/jwlawrence/wordpress-api-to-sanity
If I clone it and run it straight-up I get a
[ERR_REQUIRE_ESM]: require() of ES Module
error. So I tore it apart and put it back together. It seems to get stuck on
Schema.compile is not a function
each time I give it a go.
Does it make a difference if you run it with
sanity exec
rather than
node
?
No. I’ve got some .env variables if you’d like to try the boilerplate on your end.
I can send them over
Sure.
Hi Ryan. On line 7, could you try
const defaultSchema = Schema.default.compile({
?
That did the trick Geoff. Thanks!

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?