Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NVM is not compatible with the npm config "prefix" option warning (OS X) #1245

Closed
peteringraham opened this issue Sep 26, 2016 · 66 comments
Closed
Labels
bugs Oh no, something's broken :-( needs followup We need some info or action from whoever filed this issue/PR. OS: Mac OS shell: bash/sh

Comments

@peteringraham
Copy link

peteringraham commented Sep 26, 2016

Split off from #855 ; see that issue for a similar problem.

This is in OS X El Capitan, 10.11.6 . This error appears on starting a new shell:

nvm is not compatible with the npm config "prefix" option: currently set to "/usr/local"
Run `npm config delete prefix` or `nvm use --delete-prefix v5.7.1 --silent` to unset it.

With the usual command in the .bashrc file:

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

Running either or both of the suggested fixes don't appear to do anything, though.

Note: I foolishly installed some node version globally while trying to fix this, and also foolishly upgraded npm once in an attempt to fix this, but ran into an entirely different npm bug ( npm/npm#13918 , if you're curious ) before giving up. On the other hand, NVM seems to sorta work anyway...? Maybe?

Here's some commands & their results:

node -v
v5.7.1

nvm use 5.7.1
nvm is not compatible with the npm config "prefix" option: currently set to "/usr/local"
Run `npm config delete prefix` or `nvm use --delete-prefix v5.7.1` to unset it.

Running either or both of the suggested command doesn't change anything.

Oddly enough, the switch seems to work anyway... maybe...

nvm use 0.10
Now using node v0.10.33 (npm v1.4.28)

node -v
v0.10.33

nvm use 5.7.1
nvm is not compatible with the npm config "prefix" option: currently set to "/usr/local"
Run `npm config delete prefix` or `nvm use --delete-prefix v5.7.1` to unset it.

node -v
v5.7.1

(I guess it happened regardless??)

Debugging info:

echo $PREFIX ; echo $NPM_CONFIG_PREFIX

(nothing, just empty lines)

nvm debug
nvm --version: v0.32.0
$SHELL: /bin/bash
$HOME: /Users/pingraham
$NVM_DIR: '$HOME/.nvm'
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
nvm current: system
which node: /usr/local/bin/node
which iojs: 
which npm: /usr/local/bin/npm
npm config get prefix: /usr/local
npm root -g: /usr/local/lib/node_modules

(nvm debug, after switching)

nvm current: v0.10.33
which node: $NVM_DIR/v0.10.33/bin/node
which iojs: 
which npm: $NVM_DIR/v0.10.33/bin/npm
npm config get prefix: $NVM_DIR/v0.10.33
npm root -g: $NVM_DIR/v0.10.33/lib/node_modules

nvm ls
       v0.10.33  
         v5.7.1  
->       system  
default -> 5.7.1 (-> v5.7.1)
node -> stable (-> v5.7.1) (default)
stable -> 5.7 (-> v5.7.1) (default)
iojs -> N/A (default)
lts/* -> lts/argon (-> N/A)
lts/argon -> v4.5.0 (-> N/A)

No prefixes set in the environment, either:

$PREFIX: ''
$NPM_CONFIG_PREFIX: ''

It seems wrong to just ignore the error! Any ideas on what I could change? And is it a "Real" error, or just noise, since it seems that at least node -v acknowledges the switches anyway?

@ljharb
Copy link
Member

ljharb commented Sep 26, 2016

(also per #855 (comment), you don't have any "prefix" in any .npmrc files, and per #855 (comment), you don't have any case-insensitive form of "prefix" in env)

I assume that nvm ls, after the nvm use, has the -> pointing at "v5.7.1"?

Do you have anything in ~/.bash_profile and/or ~/.bashrc that refers to PATH, nvm, or npm?

I'm seeing a few strange things here:

  • nvm use complains about the prefix, but you don't have any prefix overrides set anywhere
  • when nvm use complains about the prefix, the node version still changes (it should be running nvm deactivate after printing that error)

@ljharb ljharb added shell: bash/sh bugs Oh no, something's broken :-( OS: Mac OS needs followup We need some info or action from whoever filed this issue/PR. labels Sep 26, 2016
@peteringraham
Copy link
Author

peteringraham commented Sep 26, 2016

I don't believe so, as far as PATH /nvm / npm references of note. My ~/.bash_profile:

source ~/.bashrc

if [ -f ~/.git-completion.bash ]; then
  . ~/.git-completion.bash
fi

The source of the bashrc seems like it shouldn't really be necessary to me, but .bashrc doesn't run if I don't include it.

The only thing in .bashrc of potential interest is pretty bog-standard:

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

eval "$(grunt --completion=bash)"

For nvm ls: Not that one version. If I use 5.7.1, it points to "system" instead (which is hopefully harmless, since node -v indicates system IS 5.7.1 ). npm -v says it is 3.6.0. If I use some other version (e.g. v.0.10.36), then it points to that, yes (and npm -v gives 2.20 ).

(after terminal start, or nvm use 5.7.1)

nvm ls
       v0.10.33  
       v0.10.36  
         v5.7.1  
->       system  
default -> 5.7.1 (-> v5.7.1)
node -> stable (-> v5.7.1) (default)
stable -> 5.7 (-> v5.7.1) (default)
iojs -> N/A (default)
lts/* -> lts/argon (-> N/A)
lts/argon -> v4.5.0 (-> N/A)

Thanks for looking into this!

@ljharb
Copy link
Member

ljharb commented Sep 26, 2016

@peteringraham hm, it's possible that v5.7.1 is corrupted? could you try nvm uninstall v5.7.1 && nvm install v5.7.1 and see if the issue is fixed?

@peteringraham
Copy link
Author

peteringraham commented Sep 27, 2016

Looks like it worked. I guess this was a side-effect of sudo npm install -g when trying to fix unrelated errors earlier.

nvm uninstall 5.7.1
file is not writable: $NVM_DIR/versions/node/v5.7.1/bin/grunt
Cannot uninstall, incorrect permissions on installation folder.
This is usually caused by running `npm install -g` as root. Run the following commands as root to fix the permissions and then try again.

  chown -R pingraham "$NVM_DIR/versions/node/v5.7.1"
  chmod -R u+w "$NVM_DIR/versions/node/v5.7.1"

After running those, uninstalling, and reinstalling, no error message, node -v returns 5.7.1.

I'll re-open if something surprising happens later, but I think this is mostly fixed.

Note that nvm ls now claims it's using "5.7.1" rather than "system", so maybe using the system version of node isn't really supported? OTOH, this might be too specific & weird a situation to bother having specific commentary on.

Thanks again for your help.

drewr added a commit to drewr/dotfiles that referenced this issue Feb 25, 2017
drewr added a commit to drewr/dotfiles that referenced this issue Feb 25, 2017
drewr added a commit to drewr/dotfiles that referenced this issue Feb 25, 2017
@elimcjah
Copy link

elimcjah commented Aug 31, 2017

@ljharb Thanks! nvm uninstall v5.7.1 && nvm install v5.7.1 worked for me.

@jedwards1211
Copy link

jedwards1211 commented Dec 12, 2017

None of the fixes here seem to have worked for me, and I get this error regardless of which version I'm trying to switch to.

However, if I merely run nvm use a second time, it succeeds. I wonder if that's why some of these fixes "worked" for other folks here? If I open a new terminal I get the error again the first time I run nvm use, and after that it's okay.

@ljharb
Copy link
Member

ljharb commented Dec 12, 2017

@jedwards1211 is there any chance you have something in your shell profile files that modifies the PATH after nvm is sourced? If so, a second nvm use would correct the PATH.

@jedwards1211
Copy link

jedwards1211 commented Dec 12, 2017

@ljharb why yes 😃 moving source nvm after all of that fixed my problem, thanks!
The offending line was pretty obvious in retrospect:

PATH="/usr/local/bin:/usr/local/opt/subversion/libexec:/opt/apache-maven-3.3.9/bin:$PATH"

@antony
Copy link

antony commented Jan 9, 2018

Wow I'm tired of this issue - seems to affect me randomly on various machines and CI environments. 3 years old and still going strong.

Not sure why it's marked as fixed? Nothing is fixed... none of the above solutions seem to work for me.

@MwumLi
Copy link

MwumLi commented Feb 28, 2018

@antony you can try the ways:

  1. comment out the code below in your shell file (such as ~/.zshrc):
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
  1. log out your account and log in
  2. uncommnet these code above and source shell file

@PascalPrecht
Copy link

PascalPrecht commented Mar 5, 2018

If you're using any other shell than bash, make sure to configure your tmux that way:

# set default shell to zsh
set -g default-command /bin/zsh
set -g default-shell /bin/zsh

This solved it for me.

@akrois
Copy link

akrois commented Mar 14, 2018

MWumLi's fix worked for me. Yes, this was highly annoying and time-consuming to fix. I'm happy to report the fix is quite simple.

@MwumLi
Copy link

MwumLi commented Mar 15, 2018

When I use tmux, the bug apear, so I view code, find the real reason is PATH overlap which result in Determine if the nvm path exists error

How to solve the bug?
There're three way:

  1. simple, but you cann't set default: nvm unalias default
  2. complex, modify function nvm_change_path:
nvm_change_path() {
  local NVM_NPM_PREFIX
  NVM_NPM_PREFIX="$(npm config --loglevel=warn get prefix)"
  # if there’s no initial path, just return the supplementary path
  if [ -z "${1-}" ]; then
    nvm_echo "${3-}${2-}"
  # if the initial path doesn’t contain an nvm path, prepend the supplementary
  # path
  elif ! (nvm_tree_contains_path "$NVM_DIR" "$NVM_NPM_PREFIX" >/dev/null 2>&1); then
    nvm_echo "${3-}${2-}:${1-}"
  # use sed to replace the existing nvm path with the supplementary path. This
  # preserves the order of the path.
  else
    nvm_echo "${1-}" | command sed \
      -e "s#${NVM_DIR}/[^/]*${2-}[^:]*#${3-}${2-}#g" \
      -e "s#${NVM_DIR}/versions/[^/]*/[^/]*${2-}[^:]*#${3-}${2-}#g"
  fi
}
  1. Hope someone can make a PR, 👏

@prachikhadke
Copy link

prachikhadke commented Apr 26, 2018

It is not fixed! i'm having the same problem for multiple versions of node. tried uninstalling and reinstalling as suggested in some of the comments above. But that did not fix it for me.

@yochem
Copy link

yochem commented May 4, 2018

nvm unalias default worked for me! Much appreciated @MwumLi 🍻

@Strongbyte-FP
Copy link

Strongbyte-FP commented May 8, 2018

The Fix
rm $(which npm) if and only if it was installed by a non-nvm node and is taking precedence over nvm's node.

You can check this by doing the following:

nvm deactivate
ls -la $(which npm)

If it points to something outside of $NVM_DIR it may be causing problems.

The explanation
I ran into this today ONLY when starting tmux.
I am using oh-my-zsh on a mac however I think the following may benefit anyone.

First, I uninstalled node from brew brew uninstall node
Then I debugged a ton and found that the npm being used was a leftover from my brew node installation.
npm was located at /usr/local/bin/npm but symlinked to /usr/local/lib/node_modules.
Because of this, the default prefix was takining over and set to /usr/local.

So I performed the following to ensure brew's node was gone:

sudo rm -rf  /usr/local/lib/node_modules
rm /usr/local/bin/npm
brew uninstall --force node
brew prune

No more errors YAY!

In hindsight, I believe all I really needed to do was either rm the npm symlink in /usr/local/bin and perhaps the npm install source at /usr/local/lib/node_modules/bin/npm (not sure of this path).

@rifler
Copy link

rifler commented May 10, 2018

@MwumLi nvm_change_path:2: command not found: npm

@MwumLi
Copy link

MwumLi commented May 12, 2018

@rifler Do you install npm? Your result show npm not be installed.

@rifler
Copy link

rifler commented May 12, 2018

globally not, only via nvm

@MwumLi
Copy link

MwumLi commented May 15, 2018

@rifler Here, first say sorry, my code has little bug.
npm config --loglevel=warn get prefix get current node path, but the above code not include the condition which npm has been not installed, so, you should add the judge: if npm not installed, this means node not be installed , you can use nvm_echo "${3-}${2-}:${1-}" directly.
I read nvm code a long time ago , so there may be some mistakes. 🤭

@derekedelaney
Copy link

derekedelaney commented Aug 22, 2019

I found that @bradydowling's fix worked for me. I recently installed yvm with brew and I guess it needed node as a dependency. Therefore I had a system node that couldn't handle the default prefix. Removing it with brew removed the error.

kkrull added a commit to kkrull/dotfiles that referenced this issue Aug 23, 2019
@TitanNano
Copy link

TitanNano commented Sep 16, 2019

I go this error because my $HOME contains a slash at the end on macOS 10.14 so my NVM_PATH looked like this /Users/username//.nvm.
Changing the NVM_PATH to export NVM_DIR="$HOME.nvm" solved the issue.

EDIT: changing the home location in the macOS user settings is actually the better solution.

@jdlam
Copy link

jdlam commented Nov 19, 2019

@TitanNano's fix worked fo rme too. Mine was set to export NVM_DIR=~/.nvm which i guess didn't have a good reference back to the real home

@jamesdixon
Copy link

jamesdixon commented Dec 29, 2019

Doing that will mean your shell starts out without any node version available, please note.

Exactly. nvm unalias default isn't a good solution

@deHelden

This comment has been minimized.

@GradedJestRisk
Copy link

GradedJestRisk commented Feb 22, 2020

I came against the same problem.
I reinstalled nvm, using zsh custom plugin, then everything went fine 😄
Here, section "As an Oh My ZSH! custom plugin"

mdb added a commit to mdb/dotfiles that referenced this issue Mar 29, 2020
@dickwyn
Copy link

dickwyn commented Apr 14, 2020

I fixed it by seeing the prefix to the current version of node that I was using
npm config set prefix $NVM_DIR/versions/node/12.16.2

@ljharb
Copy link
Member

ljharb commented Apr 14, 2020

@dickwyn that's not a good fix; you shouldn't have a prefix set at all - doing that will break as soon as you're not using v12.16.2.

@dickwyn
Copy link

dickwyn commented Apr 14, 2020

@ljharb so, should I remove the prefix with npm config delete prefix?

@ljharb
Copy link
Member

ljharb commented Apr 14, 2020

@dickwyn you should ensure that ~/.npmrc, and your env, all have no mention of prefix (in the env, in any capitalization)

@chilupa
Copy link

chilupa commented Apr 18, 2020

Do you have anything in ~/.bash_profile and/or ~/.bashrc that refers to PATH, nvm, or npm?

I had something like source ~/.nvm/nvm.sh in my ~/.zshrc. Once I removed it, i don't see the error message.

@karlmacklin
Copy link

karlmacklin commented May 5, 2020

nvm unalias default worked for me as well, thanks.

I had this problem not when launching Ubuntu (in my WSL 1 env in Win10), but when opening tmux within it. Super annoying, those error messages should be removed since doing the fixes in them didn't help at all.

@hrasoa
Copy link

hrasoa commented Jun 18, 2020

Just run into this issue, and here's what I did to fix it:

  1. remove node: brew uninstall node
  2. install desired version: nvm install 'lts/*'
  3. set the default version: nvm alias default 'lts/*'
  4. delete npm prefix: npm config delete prefix
  5. set prefix to the node version used with nvm: npm config set prefix $NVM_DIR/versions/node/v12.18.1

@ljharb
Copy link
Member

ljharb commented Jun 18, 2020

Setting the prefix like that is both unnecessary and will prevent nvm from being able to switch versions.

@hrasoa
Copy link

hrasoa commented Jun 18, 2020

@ljharb I had no issue switching from 12.x to 14.x 😉

@ljharb
Copy link
Member

ljharb commented Jun 19, 2020

@hrasoa if in node 14, npm root -g points to node 12's npm, you will have issues. Global modules must not be shared across node versions.

@tahoemph
Copy link

tahoemph commented Jul 14, 2020

The route that got me to this error was mixing installations of node based packages using nvm and brew. This can get a bit painful as installing the heroku tools or yarn will install their own version of node. I was able to resolve my issues by only ever using nvm. Big YMMV.

@tflx
Copy link

tflx commented Nov 10, 2020

I had the same issue as @radum - renaming my user folder to not include uppercase letters fixed it.
That really shouldn't be necessary IMO.

@ljharb
Copy link
Member

ljharb commented Nov 11, 2020

@tflx i agree - but did the casing of whoami and echo $HOME match? if not, fix that, as that's the issue.

@sbaraka
Copy link

sbaraka commented Nov 20, 2020

none of these work for my raspberry pi 3 running raspberry OS

@ljharb
Copy link
Member

ljharb commented Nov 20, 2020

@sbaraka then please file a new issue and file out the entire issue template. Thanks!

@mklb

This comment has been minimized.

@ljharb

This comment has been minimized.

@mklb
Copy link

mklb commented Jun 18, 2021

Good point @ljharb - the better fix was deleting the version entry in ${HOME}/.npmrc. No idea how it got there..

@lucasrueda
Copy link

lucasrueda commented Aug 17, 2021

In my case I had:

  • nvm as oh-my-zsh plugin
  • NVM_LAZY=1
  • NVM_AUTOLOAD=1

I had to remove the NVM_LAZY variable from .zshrc and everything worked

@crisdosyago
Copy link

crisdosyago commented Nov 1, 2021

If I want to run nvm in somewhere inside an npm script (or in the subprocess tree of one), then I do:

#!/bin/bash

unset npm_config_prefix

source $HOME/.nvm/nvm.sh
. $HOME/.profile

version=v14.15.3 

nvm install $version
nvm use $version

# rest of script that uses node version $version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugs Oh no, something's broken :-( needs followup We need some info or action from whoever filed this issue/PR. OS: Mac OS shell: bash/sh
Projects
None yet
Development

No branches or pull requests