CLI Error - zsh: command not found

16 replies
Last updated: Jan 29, 2022
I am all of a sudden getting zsh: command not found: sanity when trying to work with the CLI. I uninstalled the CLI and reinstalled it, still no luck. Any help?
AI Update

This is a classic PATH issue with npm global installations. The zsh: command not found: sanity error means your terminal can't find the globally installed Sanity CLI binary. Here's how to fix it:

Quick Fix

The most reliable solution is to use NVM (Node Version Manager) as it properly manages your PATH configuration:

  1. Install NVM if you haven't already:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
  1. Install Node through NVM:
nvm install 16  # or whatever version you prefer
nvm use 16
  1. Set default Node version:
nvm alias default 16
  1. Reinstall Sanity CLI:
npm install -g @sanity/cli

This should resolve the issue permanently for all new terminal sessions.

Alternative: Manual PATH Fix

If you prefer not to use NVM, you need to add npm's global bin directory to your PATH:

  1. Find your npm bin path:
npm bin -g
  1. Add it to your .zshrc (note the .bin at the end):
export PATH="/Users/yourusername/node_modules/.bin:$PATH"
  1. Reload your shell:
source ~/.zshrc

Using npx as a Workaround

While fixing your PATH, you can always run Sanity commands using npx:

npx -y sanity@latest [command]

The NVM approach is recommended because it handles all the PATH configuration automatically and avoids permission issues with global npm installations. Based on the Sanity CLI documentation, this is a common setup issue that's typically resolved by proper Node.js environment configuration.

A quick unblocker is to run
npx @sanity/cli [COMMAND]
What do you get if you run

echo $PATH
?
When I run $PATH I get /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
A quick unblocker is to run
npx @sanity/cli [COMMAND]
What do you get if you run

echo $PATH
?
And
npm get prefix -g
?
I get /Users/zachtutor/.npm-global
Should I delete npm and start all over again?
Yeah, there seems to be something going on. I’d recommend checking out https://github.com/nvm-sh/nvm
Ok, so I completely uninstalled node and then reinstalled it and i'm still getting the same issue after trying to install sanity CLI... I can't do anything without using npx
Does other global commands work?
No, I've done something somewhere wrong, nothing is working now 😬😬
This sometimes happens 😕 What happens now when you run commands?
Lots of errors, sanity cli won't even install.... I was having issues in other places than just sanity cli yesterday. Not sure what to do
What does the errors say?
So, I got really frustrated because a lot of other errors were happening and so I just totally wiped my computer and started fresh entirely. Everything works now! A little drastic, but it was driving me crazy
😅 I’m glad it’s working again
Thanks for all your help!

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?