Issue with running migrations and insufficient permissions error in Sanity.io

14 replies
Last updated: Dec 21, 2021
Just starting out with Sanity. I’m trying to run a migration (to update all the dates in one type of documents. When I try and run the migration with:
sanity exec migrations/myFile.js --with-user-token
I get an error:

node:internal/modules/cjs/loader:936
  throw err;
  ^

Error: Cannot find module '../../util/requireContext'
Require stack:
- /Users/cmv/Sites/sanity/node_modules/@sanity/core/lib/actions/exec/requireContext.js
-
...
Any help? I guess I’m not 100% sure how I set up authentication correctly.
AI Update

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 install

2. If that doesn't work, update your CLI:

# Update the global CLI
npm install -g @sanity/cli@latest

# Then update your project dependencies
npm install

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

Then run your migration:

sanity exec migrations/myFile.js --with-user-token

Consider 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!

Thanks for raising! We're getting a fix out asap!
oh, so it’s not me? 😉
Nah, we have a bug in the latest release - sorry about that
We just released v2.21.11, could you try upgrading? https://github.com/sanity-io/sanity/releases/tag/v2.21.11
That seems to have fixed the error … thanks!
I get a different one now, but I suspect that’s my fault:
ClientError: the mutation(s) failed: Insufficient permissions; permission "update" required

Yeah that seems like you might not be logged in with a user that has enough permissions (or not for this project, at least)
I am the only user on this project though. 🙂 I just set it up locally and have been poking around at things. I’m not sure why I wouldn’t be able to make changes to documents via migrations (I can via Studio).
Hi User. If you want to DM your project ID I can take a look.
Hi User! Thanks, but your colleague User already helped me out yesterday. All good now!
Great! Thanks for letting me know, and happy to hear it.
Hi, I'm using sanityClient and I'm trying to make a patch and gives me the same error about the permissions for "update". I have the token for Editor role, so can't figured out what's happening.
1.
description: "the mutation(s) failed: Insufficient permissions; permission \"update\" required"
2. items: [{,…}]

description: "the mutation(s) failed: Insufficient permissions; permission \"update\" required"
items: [{,…}]

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?