Schema type is an ES module imported through require
Schema validation reports Type appears to be an ES6 module imported through CommonJS require - use an import statement or access the `.default` property.
Despite the wording, the check doesn't look for a require call. It fires when a value in your list of schema types has no name of its own but carries a default property that looks like a type definition. That happens when a module object reaches the schema instead of the type the module exports.
Given a schema type defined as a default export:
import {defineType} from 'sanity'
export default defineType({
name: 'heroImage',
type: 'image',
})...the error appears if you register the module itself rather than its default export:
import {defineConfig} from 'sanity'
import * as heroImage from './schemaTypes/heroImage'
export default defineConfig({
projectId: 'YOUR_PROJECT_ID',
dataset: 'production',
schema: {
// `heroImage` is the module here, not the type it exports
types: [heroImage],
},
})Import the default export instead:
import {defineConfig} from 'sanity'
import heroImage from './schemaTypes/heroImage'
export default defineConfig({
projectId: 'YOUR_PROJECT_ID',
dataset: 'production',
schema: {
types: [heroImage],
},
})If you can't change the import, reach into the default export where you register the type: types: [heroImage.default].
TypeScript rejects the module form at compile time, so this error usually surfaces in JavaScript studios, or where the list of types is assembled dynamically.
In Studio v2 the same error came from a CommonJS require call in a part:@sanity/base/schema-creator file. Studio v3 and later load schema modules as ESM, where require isn't available.