Issue with running migrations and insufficient permissions error in Sanity.io
This error typically indicates a version mismatch or corrupted installation in your Sanity CLI. The requireContext module error suggests that the @sanity/core package has missing dependencies.
Based on the Sanity community thread where this exact issue was reported, this was a bug in Sanity v2.21.10 that was quickly fixed in v2.21.11.
How to fix it
1. Update your Sanity packages:
# Delete node_modules and package-lock.json
rm -rf node_modules package-lock.json
# Reinstall everything
npm install2. If that doesn't work, update your CLI:
# Update the global CLI
npm install -g @sanity/cli@latest
# Then update your project dependencies
npm install3. Check your Sanity version:
If you're still on Sanity v2, you should be aware that it's deprecated. Consider upgrading to v3 (now the current version). Check your package.json to see which version you have installed.
About authentication
You're on the right track with authentication! The --with-user-token flag is correct - it uses your logged-in user credentials. Just make sure you're logged in first:
sanity loginThen run your migration:
sanity exec migrations/myFile.js --with-user-tokenConsider using the newer migration tooling
If you continue having issues with sanity exec, I'd recommend using the newer sanity migration command instead. It's specifically built for content migrations and provides better features:
- Dry-run capabilities to test before applying changes
- Built-in batching for better performance
- Visual feedback on progress
- Better error handling
The sanity exec documentation confirms it's still useful for many tasks, but the dedicated migration tooling is now the recommended approach for content migrations specifically.
The module error you're seeing is definitely a local environment issue rather than a problem with your migration script or authentication setup, so reinstalling should resolve it!
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.