Conflict between nvm and npm prefix setting: Any solutions?

I’m having trouble using different NodeJS versions with nvm. When I try to switch versions, I get an error about the npm prefix:

$ nvm switch 4.2.4

Error: nvm can't work with the current npm prefix setting
It's set to "/Users/myuser/.npm-global"
To fix, run 'npm config delete prefix' or 'nvm switch --delete-prefix 4.2.4'

I set this prefix on purpose to avoid using sudo with npm. Is there a way to keep my global package prefix and still use nvm? I really don’t want to give up either feature. Has anyone found a workaround for this?

I’ve encountered this issue before, and it can be frustrating. One workaround I’ve found effective is to use a shell function or alias that combines the nvm switch command with resetting the npm prefix. Here’s what I do:

In my .bashrc or .zshrc, I add a function like this:

nvm_switch() {
  nvm use $1
  npm config delete prefix
  npm config set prefix $NVM_DIR/versions/node/v$1/
}

Then I use nvm_switch 4.2.4 instead of nvm switch 4.2.4. This automatically adjusts the npm prefix to match the selected Node version, avoiding conflicts while still allowing global package installs without sudo. It’s not perfect, but it’s been a decent compromise for me. Hope this helps!

hey mate, i feel ya. had similar headaches. try this: use ‘nvm install’ instead of ‘switch’. it’ll set up a fresh npm for that version. then use ‘nvm use’ to swap. might need to reinstall global pkgs, but beats the prefix mess. good luck!