Error: "Cannot use import statement outside a module" on graphql deploy

17 replies
Last updated: Jan 16, 2021
I’m experiencing an error when trying to deploy
sanity graphql deploy
and getting this error point towards a node_module
import ColorPicker from '../components/input';
^^^^^^
SyntaxError: Cannot use import statement outside a module
Does anyone know what’s causing this issue. I have been fine with this studio until the moment.
AI Update

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 --version

2. Clear and reinstall dependencies

Sometimes the issue is with a corrupted package:

rm -rf node_modules package-lock.json
npm install

3. 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 update

4. 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 thread
17 replies
It would appear so, but I can't see any direct imports in the published package... Could you paste the output of what
sanity versions
gives you when you run it in the studio folder?
@sanity/cli 2.1.4 (up to date)@sanity/base 2.1.6 (up to date)
@sanity/cli 2.1.4 (up to date)
@sanity/color-input 2.1.4 (up to date)
@sanity/components 2.1.4 (up to date)
@sanity/core 2.1.4 (up to date)
@sanity/default-layout 2.1.6 (up to date)
@sanity/default-login 2.1.4 (up to date)
@sanity/desk-tool 2.1.6 (up to date)
@sanity/vision 2.1.4 (up to date)
/Users/dustinsimensen/Documents/Final/studio/node_modules/sanity-plugin-color-picker/lib/schema/index.js:82
import ColorPicker from '../components/input';
^^^^^^
SyntaxError: Cannot use import statement outside a module

thats the full error
I had to completely remove it to move on
Oh, I see,
sanity-plugin-color-picker
is a community-contributed plugin - it seems to not compile the javascript sources to ES5 😕
I'll see if I can send a pull request to fix it, but I can't guarantee a swift resolution given it's not an official plugin
That’s okay, I don’t know why it’s there, honestly?
I just deleted it and two pieces of code that used it and didn’t do anything. Thanks.
I'm not sure? Was this project based on one of our starters?
It was not, no.
A teammate put the code in last night and it crashed my sanity
Aha
May I ask an unrelated question about Sanity Images inside the CMS?
Or should I ask in main channel?
Main channel would be best 🙂
Main channel would be best 🙂
[ for what it's worth, this is now fixed in the latest release of the mentioned plugin - https://github.com/edolyne/sanity-plugin-color-picker/pull/2 ]

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?