nvm-sh / nvm

Node Version Manager - POSIX-compliant bash script to manage multiple active node.js versions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

peteringraham opened this issue Β· comments

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?

(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)

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!

@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?

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.

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

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.

@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.

@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"

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.

@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

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.

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.

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, πŸ‘

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.

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

commented

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).

@MwumLi nvm_change_path:2: command not found: npm

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

globally not, only via nvm

@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. 🀭

commented

@Strongbyte-FP This was exactly my problem - thank you!

I was able to leave the system node on and simply remove the npm install that it came with (might not need the system node anyway so it's probably going out - it might be that something needed it - cannot remember....)

rm -rf /usr/local/lib/node_modules/npm

That was enough for me.

I tried all but still not work for me.
I use where node and found I have a node of global in /usr/local/bin/node.
then I use this to delete it

sudo rm -rf /usr/local/lib/node_modules
sudo rm /usr/local/bin/node
cd  /usr/local/bin && ls -l | grep "../lib/node_modules/" | awk '{print $9}'| xargs rm

and then, it work for me!!!

I realised, that this behaviour connected with commitizen
when I simply run git commit -am "msg" - all hooks run fine
when I run git-cz - I get

nvm is not compatible with the "npm_config_prefix" environment variable: currently set to "/Users/rifler/.nvm/versions/node/v10.6.0"
Run `unset npm_config_prefix` to unset it.
husky > can't find npm in PATH, skipping precommit script in package.json
nvm is not compatible with the "npm_config_prefix" environment variable: currently set to "/Users/rifler/.nvm/versions/node/v10.6.0"

@rifler are you using husky along with commitizen, by chance?

the latest version of husky (and many previous versions) leverages nvm to run your tests in the version of node defined in your .nvmrc. the newest versions, that haven't hit latest yet, don't do this anymore (i think because it is assumed that if you are using nvm that you are already running the correct version through other means), so they avoid this problem.

try npm install husky@next -D if this does apply to you and see if it resolves this particular issue.

@travi it helps, thank you!

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

Thanks. This one helped for me.

nvm unalias default

Thanks. Worked for me.

I just close the terminal by right clicking the icon below and clicking on force quit and reopened it. This worked for me.

@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

This worked for me! Thanks!

Not sure what installed a global npm for me, but I ran this to have npm uninstall itself

Note: I had to run this twice.

# Run until which npm returns: npm not found
npm un -g npm
which npm

Afterwards I uninstalled my current nvm node version I was on, and then reinstalled it to get a standard npm back and running.

CURRENT_NODE_VERSION="$(nvm version)"
nvm use vSOMETHING_ELSE
nvm uninstall $CURRENT_NODE_VERSION
nvm install $CURRENT_NODE_VERSION

Hope this helps someone...

@ctsstc global npm comes with node, and you should never uninstall it - the only cure is reinstalling node to get it back.

@ljharb if you're using nvm it should be located under your nvm versions like:

/Users/codyswartz/.nvm/versions/node/v11.12.0/bin/npm

Not something like the following that mine was pointed at:

/usr/local/bin/npm

Yet when I ran which node it was pointing at nvm, so that's why I was confused where the rouge global nvm came from. It might have been from some ruby gem I installed recently. I treat node installs Ephemerally so reinstalling isn't much concern for me.

nvm unalias default worked for me! Much appreciated @MwumLi 🍻
this is the solution :)

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

@ljharb I'm thinking the same thing. Removing the default alias fixed this but shouldn't there be another way around this?

Update: As a workaround, I removed the default alias and then added the following to my .bashrc file.

nvm use --delete-prefix v8.16.0

That did the trick but then I decided to give the chosen answer here one more shot (which I'm sure I had done in the past) and it actually worked for me.

$ npm config delete prefix
$ npm config set prefix $NVM_DIR/versions/node/v8.16.0 # Where v8.16.0 is the version you want

If this doesn't work for you then try unsetting default and then adding the --delete-prefix line I included above in your .bashrc or .zshrc file.

I fixed this by deleting a ~/.huskyrc file. Not sure how it got there, but I tested my hooks after deleting it and they still work.

Pretty simple fix

Navigate to your .nvm folder remove all npm version, since you can just reinstall with nvm easily then run unset npm_prefix_config, then install some arbitrary npm version using nvm, then run nvm use {version you installed}

Boom npm is usable again.

EDIT:

My Mistake the final command you must run is

ln -s $(which node) /usr/local/bin/node

which should put the current node being used in the path.

In my case, I just uninstalled nodejs installed by system (sudo apt uninstall nodejs), removed ~/.nvm folder, cleaned up .bashrc and .zshrc from nvm related commands, then installed nvm again.

Fro those coming here and you are using a Mac, my issue was that my user folder had upper case letters in it, but my profile folder setting under users pointed to the all lower case path version of it. Once I logged in from another account renamed to have all lower case all worked just fine.

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.

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.

@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

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

Exactly. nvm unalias default isn't a good solution

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

Exactly. nvm unalias default isn't a good solution

Still a good solution :)

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"

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

@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.

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

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

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.

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.

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

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

@ljharb I had no issue switching from 12.x to 14.x πŸ˜‰

@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.

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.

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.

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

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

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

Just setting the prefix to the nvm version I use helped: npm config set prefix $NVM_DIR/versions/node/v14.15.3 Replace the version with yours of course πŸ‘πŸ»

@mklb definitelt don’t do that; that will break the moment you try to change node versions. Prefix should never be set at all.

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

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

commented

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