npm install Sanity CLI but "command not found: sanity" error

11 replies
Last updated: Dec 19, 2021
Hello everyone, I just can’t get the sanity cli up and running.
After typing in

npm install -g @sanity/cli
I only get the following output

changed 1 package, and audited 2 packages in 2s

found 0 vulnerabilities
after typing in
sanity
I get the following error message
zsh: command not found: sanity
AI Update

This is a common issue on macOS with zsh! The problem is that npm's global binary directory isn't in your $PATH environment variable, so your shell can't find the sanity command.

Based on this community discussion about the exact same issue, the recommended solution is to use NVM (Node Version Manager) to manage your Node.js installation.

The Solution: Use NVM

  1. Install NVM by following the instructions at https://github.com/nvm-sh/nvm

  2. Install Node.js through NVM:

    nvm install 16  # or any recent version like 18, 20
    nvm use 16
  3. Set the default Node version so you don't have to run nvm use every time you open a new terminal:

    nvm alias default 16
  4. Reinstall the Sanity CLI:

    npm install -g @sanity/cli

Now the sanity command should work in any new terminal window!

Why This Happens

When you install Node.js directly (not through NVM), the npm global binary path sometimes doesn't get properly added to your shell's $PATH. This is especially common on macOS with zsh. NVM handles this path configuration automatically and also makes it easier to manage multiple Node.js versions.

Alternative Quick Fix (Without NVM)

If you prefer to stick with your current Node.js installation, you can manually add the npm bin directory to your PATH:

  1. Run npm bin -g to find your global npm bin directory (likely something like /Users/yourusername/node_modules/.bin)
  2. Add this line to your ~/.zshrc file:
    export PATH="/Users/yourusername/node_modules/.bin:$PATH"
    Make sure to include the .bin part at the end!
  3. Restart your terminal or run source ~/.zshrc

However, using NVM is generally more reliable and is the approach recommended by the Sanity community for avoiding these types of PATH issues. Note that Sanity Studio v4 requires Node.js 20 or higher, so you'll want to use a recent Node version anyway.

Show original thread
11 replies
Looks like your npm binary path is not part of
$PATH
indeed.
Not sure how familiar you are with node and npm, but I tend to recommend that people use a node versioning manager like NVM (
https://github.com/nvm-sh/nvm ).
Anyway, you should be able to run
npm bin
to see the path that you need to add to your
$PATH
. Temporarily you could do something like:

export PATH=$(npm bin):$PATH
Which should fix it for the current terminal session. Or you can add it to your shell environment for a more long-term solution. (This is usually taken care of during the node.js installation process, though)
If I run
npm bin
I get the response
/Users/henryauffahrt/node_modules/.bin

I put in my .zshrc file the following line
export PATH="/Users/henryauffahrt/node_modules/:$PATH"

But the errors stay the same
I just installed nvm as you recommend. But the error stays the same 😞
But running
/Users/henryauffahrt/node_modules/.bin/sanity
works?
I complete throw away my ~/.zshrc config.. it was a mess build up over the years. I also throw away my oh-my-zsh folder. And build up everything new. Currently try to install sanity cli but get an EACCES error. But I don’t want to install it as superuser. Found this documentation to solve it: https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally
There I found this information in the very beginning “Reinstall npm with a node version manager (recommended),”
The same you recommend me. I already installed nvm. But my current session is not using npm over nvm… after type in
nvm use 16
it works!
But I’m wondering how could I make sure in a new terminal windows session that it will use the nvm version as default?
also on a new terminal windows I need to type in
nvm use16
before I can use
sanity
This is the solution:
nvm alias default 16
Ah, ok. Glad you got it figured out!
Thanks for your help
user Z
!

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?