How to resolve nvm conflict with custom npm prefix configuration

I’m encountering an issue when attempting to switch Node.js versions using nvm. Every time I try to change versions, I get an error message saying that nvm doesn’t work with the npm prefix setting that I have configured.

The error looks like this:

$ nvm switch v4.2.4

nvm is not compatible with the npm config "prefix" option: 
   currently set to "/Users/myuser/.npm-packages"
Run `npm config delete prefix` or `nvm switch --delete-prefix v4.2.4` to unset it.

I intentionally configured this prefix setting because I want to install global npm packages without needing sudo permissions. This helps me avoid permission issues when installing packages globally.

Is there a solution that allows me to keep using nvm for managing Node.js versions while maintaining my custom prefix configuration for global package installations? I don’t want to lose the ability to install packages globally without administrator privileges.

Actually ran into this myself last year and there’s another approach that might work better for your workflow. Instead of deleting the prefix entirely, you can create a simple shell function that temporarily unsets the prefix when switching node versions. I added this to my bashrc: nvm() { npm config delete prefix 2>/dev/null; command nvm "$@"; }. This automatically handles the prefix deletion whenever you use nvm commands, then you can set your prefix back afterwards if needed. The beauty is that nvm’s own directory structure under ~/.nvm/versions will handle the global packages properly for each node version anyway, so you might find you don’t actually need the custom prefix anymore once you’re using nvm consistently. Each node version gets its own isolated global package space which is honestly better than having everything mixed together in one custom directory.

honestly just use the --delete-prefix flag thats mentioned in your error message. yeah its a bit annoying but it works fine and you dont have to mess with permissions or shell configs. once nvm switches versions it handles global packages per version anyway so your original prefix setup becomes kinda redundant tbh

Had this exact same problem about six months ago and it drove me crazy for hours. The core issue is that nvm manages its own global package directories for each Node version, so having a custom prefix actually interferes with how it isolates different Node environments. What worked for me was deleting the prefix setting and then adjusting my shell permissions instead. You can run npm config delete prefix and then change the ownership of the npm directories with sudo chown -R $(whoami) ~/.npm. This way nvm can do its thing properly while you still avoid sudo for global installs. The alternative approach using --delete-prefix flag mentioned in your error works too, but you’ll need to remember to use it every time you switch versions which gets annoying fast. Trust me, fixing the underlying permissions is much cleaner than working around the prefix conflict repeatedly.