nvm-sh / nvm

Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions
MIT License
78.95k stars 7.9k forks source link

Error message: `nvm is not compatible with the npm config "prefix" option: currently set to "/opt/homebrew"` #2881

Closed samholmes closed 11 months ago

samholmes commented 2 years ago

Operating system and version:

nvm debug output:

```sh nvm --version: v0.36.0 $SHELL: /bin/zsh $SHLVL: 2 whoami: 'samholmes' ${HOME}: /Users/samholmes ${NVM_DIR}: '${HOME}/.nvm' ${PATH}: /opt/homebrew/bin:/opt/homebrew/sbin:${HOME}/.deno/bin:/usr/local/sbin:${HOME}/bin:/opt/homebrew/bin:/opt/homebrew/sbin:${HOME}/.deno/bin:/usr/local/sbin:${HOME}/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin:/Library/Apple/usr/bin:${HOME}/.cargo/bin:/Applications/kitty.app/Contents/MacOS:/usr/local/bin:${HOME}/Library/Android/sdk/emulator:${HOME}/Library/Android/sdk/tools:${HOME}/Library/Android/sdk/tools/bin:${HOME}/Library/Android/sdk/platform-tools:/opt/homebrew/bin:${HOME}/Library/Android/sdk/emulator:${HOME}/Library/Android/sdk/tools:${HOME}/Library/Android/sdk/tools/bin:${HOME}/Library/Android/sdk/platform-tools $PREFIX: '' ${NPM_CONFIG_PREFIX}: '' $NVM_NODEJS_ORG_MIRROR: '' $NVM_IOJS_ORG_MIRROR: '' shell version: 'zsh 5.8.1 (x86_64-apple-darwin21.0)' uname -a: 'Darwin 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:28:23 PDT 2022; root:xnu-8020.141.5~2/RELEASE_ARM64_T6000 arm64' checksum binary: 'shasum' OS version: macOS 12.5.1 21G83 curl: /usr/bin/curl, curl 7.79.1 (x86_64-apple-darwin21.0) libcurl/7.79.1 (SecureTransport) LibreSSL/3.3.6 zlib/1.2.11 nghttp2/1.45.1 wget: not found sed: /usr/bin/sed cut: /usr/bin/cut basename: /usr/bin/basename rm: /bin/rm mkdir: /bin/mkdir xargs: /usr/bin/xargs git: /usr/bin/git, git version 2.32.1 (Apple Git-133) grep: /usr/bin/grep, grep (BSD grep, GNU compatible) 2.6.0-FreeBSD awk: /usr/bin/awk, awk version 20200816 nvm current: system which node: /opt/homebrew/bin/node which iojs: iojs not found which npm: /opt/homebrew/bin/npm npm config get prefix: /opt/homebrew npm root -g: /opt/homebrew/lib/node_modules ```

nvm ls output:

```sh v10.23.0 v12.19.0 v14.15.0 v14.17.5 v16.15.1 -> system default -> lts/* (-> v16.15.1) node -> stable (-> v16.15.1) (default) stable -> 16.15 (-> v16.15.1) (default) iojs -> N/A (default) unstable -> N/A (default) lts/* -> lts/gallium (-> v16.15.1) lts/argon -> v4.9.1 (-> N/A) lts/boron -> v6.17.1 (-> N/A) lts/carbon -> v8.17.0 (-> N/A) lts/dubnium -> v10.24.1 (-> N/A) lts/erbium -> v12.22.12 (-> N/A) lts/fermium -> v14.19.3 (-> N/A) lts/gallium -> v16.15.1 ```

How did you install nvm?

install script in readme

What steps did you perform?

  1. Open terminal app
  2. Open a sub-shell with zsh
  3. Run nvm ls

What happened?

On step 2, an error message appears:

nvm is not compatible with the npm config "prefix" option: currently set to "/opt/homebrew"
Run `npm config delete prefix` or `nvm use --delete-prefix v16.15.1 --silent` to unset it.
Make sure your username (samholmes) matches the one in your $HOME path.
See the "macOS Troubleshooting" section in the docs for more information.

And, on step 3, the selected node version is system.

What did you expect to happen?

No message is shown when the sub-shell starts and the selected node version should be v16.15.1.

Is there anything in any of your profile files that modifies the PATH?

ljharb commented 2 years ago

I suspect your node-installed homebrew might come with a hardcoded npmrc that sets prefix. What output and exit codes do you get from the following commands:

samholmes commented 2 years ago
❯ nvm_npmrc_bad_news_bears /opt/homebrew/lib/node_modules/npm/npmrc                                                                                                    
zsh: command not found: nvm_npmrc_bad_news_bears
ljharb commented 2 years ago

hm, that suggests that zsh isn't even sourcing nvm.sh at all. Can you . $NVM_DIR/nvm.sh and try again?

samholmes commented 2 years ago

@ljharb I did . $NVM_DIR/nvm.sh and then ran which nvm_npmrc_bad_news_bears without any avail. I took a peak in the nvm.sh file and found no reference to nvm_npmrc_bad_news_bears.

ljharb commented 2 years ago

which won't ever work with nvm, nvm is a bunch of sourced shell functions. After sourcing the file, you should just be able to run it.

samholmes commented 2 years ago

which does work for other shell functions such as nvm_install_latest_npm (it writes out the function implementation). I ran nvm_npmrc_bad_news_bears again after sourcing and still got the same results.

ljharb commented 2 years ago

ah, that's a zsh-specific behavior then :-) use command -v to be posix-compliant.

I think the issue is that you're using an ancient version of nvm - v0.36.0. Can you upgrade to v0.39.1?

samholmes commented 2 years ago

Upgrading got ride of the "prefix" message, though the default node is still system from sub-shells. What determines the default node version used?

ljharb commented 2 years ago

nvm alias default node will set it to "node" (ie, the latest available); or, if you have it in a .nvmrc file in the current directory or any parent directory, its contents will determine it.

If no default alias or nvmrc is available, the default is system.

samholmes commented 2 years ago

While in a sub-shell (running zsh), nvm ls shows:

       v10.23.0
       v12.19.0
       v14.15.0
       v14.17.5
       v16.15.1
->       system
default -> lts/* (-> v16.15.1)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v16.15.1) (default)
stable -> 16.15 (-> v16.15.1) (default)
lts/* -> lts/gallium (-> v16.15.1)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.19.3 (-> N/A)
lts/gallium -> v16.15.1

and running nvm use 16 does not change the selection; which node prints /opt/homebrew/bin/node and node --version prints v18.0.0.

This is what I have in my .zshrc file:

# NVM
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

No other reference to NVM in .zprofile or .zenv. I have no .nvmrc file.

ljharb commented 1 year ago

Sorry for the delay here.

What, specifically, do you have in your profile files that alters the PATH after those nvm lines? In particular, if anything homebrew-related prepends itself to the PATH afterwards, nvm will never be able to take over from homebrew's node.

samholmes commented 1 year ago

After NVM_DIR settings in my .zshrc:

export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
ljharb commented 1 year ago

This is very confusing. What does env | grep -i prefix print out?

samholmes commented 1 year ago
HOMEBREW_PREFIX=/opt/homebrew

Same print in both regular shell, and sub-shell (i.e. after running zsh).

ljharb commented 1 year ago

aha! the error message above says the "npm config" prefix option. Is there an .npmrc in your project dir, or in $HOME/.npmrc?

samholmes commented 1 year ago

There is a $HOME/.npmrc file, but it just contains:

//registry.npmjs.org/:_authToken=***
samholmes commented 1 year ago

I did brew uninstall node and now nvm will always take precedent over "system" because /opt/homebrew/bin (in the path) does not contain a node bin

ljharb commented 1 year ago

That's certainly a workaround, but nvm should be putting its own directories first in the PATH, and the homebrew ones shouldn't be added after it.

BogdanMaier commented 11 months ago

I found this in my .zshrc:


#
# homebrew setup, following https://noahpeeters.de/posts/apple-silicon/homebrew-setup/
#
if [ -d "/opt/homebrew/bin" ]; then
    export PATH="/opt/homebrew/bin:$PATH"
fi

function ibrew() {
   arch --x86_64 /usr/local/bin/brew $@
}

# variables needed to properly install things under intel or m1

ARCH="$(uname -m)"
case ARCH in
    i386)   ARCH="386" ;;
    i686)   ARCH="386" ;;
    x86_64) ARCH="amd64" ;;
    arm)    dpkg --print-architecture | grep -q "arm64" && ARCH="arm64" || ARCH="arm" ;;
esac

# commenting this out removed the error
# if [[ "${ARCH}"  == "arm64" ]]; then
#     PREFIX="/opt/homebrew"
# else
#     PREFIX="/usr/local"
# fi
ljharb commented 11 months ago

I think this has been answered.

bgever commented 6 months ago

I got this error with zsh because I load the homebrew environment variables with this command in .zprofile:

eval "$(/opt/homebrew/bin/brew shellenv)"

And was loading NVM in .zshenv per the suggestion from React Native so that Xcode can find node:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

I resolved it by adding the unset command as the NVM output suggests:

unset PREFIX # Workaround for homebrew
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm

In fact, I switched to https://github.com/lukechilds/zsh-nvm, which loads NVM as a zsh plugin (using manual installation to ~/.zsh-nvm/), and now get autocomplete and lazy loading. 🎉 In .zhenv:

# NVM as zsh plugin: https://github.com/lukechilds/zsh-nvm
# Upgrade NVM with `nvm upgrade`
unset PREFIX # Workaround for homebrew
export NVM_COMPLETION=true
export NVM_LAZY_LOAD=true
export NVM_LAZY_LOAD_EXTRA_COMMANDS=('yarn')
source ~/.zsh-nvm/zsh-nvm.plugin.zsh