Error running "dev" command in Sanity project

15 replies
Last updated: Jan 18, 2023
Hiya! Just started my project and i have this error:Error: Command "dev" is not available outside of a Sanity project context.
Run the command again within a Sanity project directory, where "sanity"
is installed as a dependency.
at CommandRunner.runCommand (~/Documents/GitHub/regi-khan/studio/node_modules/@sanity/cli/lib/_chunks/cli-0bbdf4ec.js
:17877:33701) at Object.runCli (~/Documents/GitHub/regi-khan/studio/node_modules/@sanity/cli/lib/_chunks/cli-0bbdf4ec.js
:17880:2456)
Don't understand, it defintely has sanity as a dependency....
AI Update

This error happens when you're running the sanity dev command from the wrong directory. Even though you have Sanity installed as a dependency, the CLI needs to be executed from the actual Sanity project folder.

Here are the most common causes and fixes:

1. Wrong directory (most likely) Make sure you're running the command from inside the studio folder. Based on your error path, you should be in:

cd ~/Documents/GitHub/regi-khan/studio
sanity dev

2. Missing sanity.config.ts/js The CLI looks for a Sanity config file to confirm it's in a project context. Verify you have a sanity.config.ts (or .js) file in your studio directory.

3. Corrupted node_modules Sometimes the dependency installation gets messed up. Try:

cd studio  # or your project folder
rm -rf node_modules
npm install
# or yarn install / pnpm install
sanity dev

4. Running from monorepo root If you're in a monorepo setup and trying to run commands from the root, you have two options:

  • Navigate into the studio folder first (recommended)
  • Use the --cwd flag: sanity dev --cwd ./studio

5. Global vs local CLI If you have the Sanity CLI installed globally but your project uses a different version locally, this can cause issues. Always use the local version:

npx sanity dev
# or if using npm scripts
npm run dev

The most likely culprit based on your path structure (~/Documents/GitHub/regi-khan/studio/node_modules/...) is that you're running the command from the parent regi-khan directory instead of from inside the studio folder. Try cd studio first and then run sanity dev again!

Are you running the command from the right directory?
Hi
user M
! Yes indeed. So weird. Just checked and this only happens with the projects where i have sanity v3. Does that make sense?
user M
Here is a screenshot, hope it helps.Thanks so much!!
Very weird! What version of the CLI are you running?
"version": "3.2.3"
I know :face_with_peeking_eye:
What does your
package.json
look like?
Yes, it looks like this:
{

"private": true,

"name": "blog-with-gatsby-studio",

"main": "package.json",

"author": "Sanity <<mailto:hello@sanity.io|hello@sanity.io>>",

"scripts": {

"start": "sanity start",

"build": "sanity build",

"dev": "sanity dev",

"format": "prettier-eslint --write \"**/*.js\" \"!node_modules/**\"",

"graphql-deploy": "sanity graphql deploy",

"lint": "eslint .",

"test": "sanity check"

},

"keywords": [

"sanity"

],

"dependencies": {

"@react-md/icon": "^5.1.3",

"@sanity/dashboard": "^3.1.1",

"@sanity/vision": "^3.2.4",

"npm5": "^5.0.0-beta.69",

"prop-types": "^15.8.1",

"react": "^18.2.0",

"react-dom": "^18.2.0",

"react-icons": "^4.7.1",

"sanity": "^3.2.4",

"sanity-plugin-dashboard-widget-netlify": "^2.0.1",

"styled-components": "^5.3.3"

},

"devDependencies": {

"@babel/core": "^7.20.7",

"@babel/eslint-parser": "^7.19.1",

"eslint": "^8.31.0"

}

}
Maybe try deleting node modules and your package-lock then try reinstalling?
I did that before, but what actually solved it is that i created a new sanity studio project and after running that, the rest runned okay
no idea what happened really
So weird!!
i know 😞 was mega stressed. Happy that is solved now. Thanks for your help as well 💛 have a good night!
You too!

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?