Error: "Cannot use import statement outside a module" on graphql deploy
This "Cannot use import statement outside a module" error during sanity graphql deploy is a classic Node.js module system conflict. The issue occurs when the GraphQL deploy process tries to load your schema (including all custom input components), and encounters ES module syntax (import/export) in a context expecting CommonJS.
Based on the Sanity documentation on this error, here's what's happening and how to fix it:
The Root Cause
The error points to a ColorPicker component using ES module syntax. This typically happens when you have a schema type definition like:
export default {
// ... type definition
}But you're importing it using CommonJS require():
import createSchema from 'part:@sanity/base/schema-creator'
export default createSchema({
name: 'my-schema',
types: schemaTypes.concat([
require('./someTypeDef.js') // ← This is the problem
])
})The Solution
Use ES module import statements instead of require():
import createSchema from 'part:@sanity/base/schema-creator'
import someTypeDef from './someTypeDef'
export default createSchema({
name: 'my-schema',
types: schemaTypes.concat([
someTypeDef // ← Much better
])
})Additional Troubleshooting Steps
1. Check your Node.js version
If you're using Sanity Studio v4, you need Node.js 20 or higher:
node --version2. Clear and reinstall dependencies
Sometimes the issue is with a corrupted package:
rm -rf node_modules package-lock.json
npm install3. Look for color picker plugins
Check your package.json for packages like:
sanity-plugin-color-input@sanity/color-input- Any custom input components
Try updating them:
npm update4. Review your schema files
Search your schema files for any require() statements and replace them with proper import statements at the top of the file.
The GraphQL API deployment command needs to parse your entire schema, so any module system mismatches will cause this error. Once you've converted all your schema imports to use ES modules consistently, the deploy should work fine.
If the issue persists after these fixes, share your schema configuration and the specific file paths mentioned in the error for more targeted help!
Show original thread17 replies
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.