👀 Our most exciting product launch yet 🚀 Join us May 8th for Sanity Connect

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!
Feb 4, 2022, 6:56 PM
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);
Feb 4, 2022, 6:58 PM
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.
Feb 4, 2022, 10:32 PM
Now that I type that out loud I realize this may be why it’s not working.
Feb 4, 2022, 10:33 PM
I’m working on building a migration process using this for reference: https://github.com/jwlawrence/wordpress-api-to-sanity
Feb 4, 2022, 10:33 PM
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.
Feb 4, 2022, 10:41 PM
Does it make a difference if you run it with
sanity exec
rather than
node
?
Feb 4, 2022, 10:50 PM
No. I’ve got some .env variables if you’d like to try the boilerplate on your end.
Feb 4, 2022, 10:53 PM
I can send them over
Feb 4, 2022, 10:53 PM
Sure.
Feb 4, 2022, 10:53 PM
Hi Ryan. On line 7, could you try
const defaultSchema = Schema.default.compile({
?
Feb 5, 2022, 4:21 AM
That did the trick Geoff. Thanks!
Feb 5, 2022, 12:29 PM

Sanity– build remarkable experiences at scale

Sanity is a modern headless CMS that treats content as data to power your digital business. Free to get started, and pay-as-you-go on all plans.

Was this answer helpful?