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 use" not persisting #658

Closed
YourDeveloperFriend opened this issue Feb 12, 2015 · 62 comments
Closed

"nvm use" not persisting #658

YourDeveloperFriend opened this issue Feb 12, 2015 · 62 comments

Comments

@YourDeveloperFriend
Copy link

Sorry, I'm sure this is a duplicate, but I can't find anything in the miriad of issues. I'm running on Ubuntu 14.04, nvm version 0.23.3, and I just recently installed nvm. I used to be on n but I switched here in order to try out iojs, so it's possible that some residual n is conflicting with nvm. I removed all the node versions with n except 0.12.0, uninstalled n, installed nvm, and I properly sourced nvm as mentioned in issue #394. Then I ran into the following issue:

$ nvm current
v0.12.0
$ nvm install 0.10
######################################################################## 100.0%
Now using node v0.10.36
$ nvm current # NOTE: didn't change
v0.12.0
$ node -v
v0.12.0
$ nvm use 0.10
Now using node v0.10.36
$ nvm current
v0.12.0
$ node -v
v0.12.0
$ nvm install iojs
######################################################################## 100.0%
WARNING: checksums are currently disabled for io.js
Now using io.js v1.2.0
$ nvm current
iojs-v1.2.0
$ node -v # NOTE: didn't change AND different from nvm current
v0.12.0

This all works just fine when run in sudo su, but when I exit that shell, it doesn't permeate.

More possible clues:

$ nvm run 0.10 -v
Running node v0.10.36
v0.12.0
$ nvm exec 0.10 node -v
Running node v0.10.36
v0.10.36
$ nvm run iojs -v
Running io.js v1.2.0
v1.2.0

Not sure how to proceed.

@ljharb
Copy link
Member

ljharb commented Feb 12, 2015

nvm use isn't meant to persist - it's only for the lifetime of the shell.

You can either do nvm alias default node if you want that to be the default when opening new shells, or, you can make a .nvmrc file that will take precedence anywhere in the current directory, upwards to /.

@ljharb ljharb closed this as completed Feb 12, 2015
@YourDeveloperFriend
Copy link
Author

I don't understand. It's not persisting at all, let alone for the lifetime of the shell. Please read the issue again:

This is all one shell:

$ nvm current
v0.12.0
$ nvm install 0.10
######################################################################## 100.0%
Now using node v0.10.36
$ nvm current # NOTE: didn't change
v0.12.0
$ node -v
v0.12.0
$ nvm use 0.10
Now using node v0.10.36
$ nvm current
v0.12.0
$ node -v
v0.12.0
$ nvm install iojs
######################################################################## 100.0%
WARNING: checksums are currently disabled for io.js
Now using io.js v1.2.0
$ nvm current
iojs-v1.2.0
$ node -v # NOTE: didn't change AND different from nvm current
v0.12.0

You may have been confused by this comment:

This all works just fine when run in sudo su, but when I exit that shell, it doesn't permeate.

In other words, it works as intended within sudo, but not in my regular shell.

@ljharb
Copy link
Member

ljharb commented Feb 13, 2015

Ah, sorry, responding too quickly :-)

What does which node, echo $PATH, which iojs, and npm root -g report at all those steps?

@ljharb ljharb reopened this Feb 13, 2015
@ljharb
Copy link
Member

ljharb commented Feb 13, 2015

Also, how did you install nvm? (Note that homebrew is not supported or recommended)
Under what user did you install nvm? (nvm must be installed once for each user account you want to use with it; it's not meant for being shared across accounts)

@YourDeveloperFriend
Copy link
Author

$ which node
/home/yourdeveloperfriend/.nvm/versions/node/v0.12.0/bin/node
$ echo $PATH
/home/yourdeveloperfriend/.nvm/versions/node/v0.12.0/bin:/home/yourdeveloperfriend/.rbenv/plugins/ruby-build/bin:/home/yourdeveloperfriend/.rbenv/shims:/home/yourdeveloperfriend/.rbenv/bin:/usr/local/heroku/bin:/home/yourdeveloperfriend/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/yourdeveloperfriend/.rvm/bin:~/www/phantomjs/bin:/home/yourdeveloperfriend/.rvm/bin
$ which iojs # no response.
$ npm root -g
/home/yourdeveloperfriend/.nvm/versions/node/v0.12.0/lib/node_modules

I installed nvm via my one user: yourdeveloperfriend. I installed with the install script: curl https://raw.githubusercontent.com/creationix/nvm/v0.23.3/install.sh | bash - I'm on Ubuntu 14.04. I don't have any other user accounts.

@YourDeveloperFriend
Copy link
Author

And don't worry about closing the issue too quickly, I understand you guys probably have a lot of garbage to sort through. Thanks for your help.

@ljharb
Copy link
Member

ljharb commented Feb 13, 2015

@YourDeveloperFriend When you're in the above situation (where 0.10 and 0.12 are installed, 0.12 is active, and nvm use 0.10 doesn't seem to have any effect), what happens if you do nvm deactivate and then check which node ; node -v?

@YourDeveloperFriend
Copy link
Author

$ nvm deactivate
/home/yourdeveloperfriend/.nvm/*/bin removed from $PATH
/home/yourdeveloperfriend/.nvm/*/share/man removed from $MANPATH
$ which node
/usr/local/bin/node
$ node -v
0.12.0

@ljharb
Copy link
Member

ljharb commented Feb 13, 2015

This is very confusing :-) so, when you start with nvm deactivate, your PATH should be such that nvm current reports "system" (ie, /usr/local/bin/node). From there, nvm use iojs for example should activate the latest io.js version, and your PATH should be such that nvm current and node -v report that io.js version, and so on. From there, nvm use 0.10 should remove the io.js path from your PATH, and then add the 0.10 version to it, and then nvm current and node -v should report the 0.10 version.

The only thing I can thing of is that somewhere in that chain, the PATH changes aren't happening - either the removal, or the addition.

What's the output of alias? Just want to make certain that you don't have any builtins aliased - even though we use command internally for most of them to bypass aliases.

@YourDeveloperFriend
Copy link
Author

That was it! I had an alias in my .bashrc setting node='nodejs'. I think I remember needing to put that in there when I first installed nodejs... But why? I guess this has nothing to do with nvm. Thanks!

@ljharb
Copy link
Member

ljharb commented Feb 13, 2015

ah HA! Yay, thanks for working through it with me!

If you originally installed node via apt, the default Ubuntu package is called "nodejs", so I suspect that's the origin.

@ljharb ljharb closed this as completed Feb 13, 2015
@YourDeveloperFriend
Copy link
Author

So should I uninstall node via apt? I feel uncomfortable about there being two versions of node on my system, that may be confusing in the future...

@ljharb
Copy link
Member

ljharb commented Feb 13, 2015

Yes, absolutely. It won't cause problems, but as we've seen it could definitely cause confusion :-)

@fergiemcdowall
Copy link

Just a not to say that I had been struggling with this for a long time on ubuntu and removing alias node='nodejs' from .bashrc fixed it.

(In pre-nvm days adding the alias to .bashrc was the recommended way of getting to node to run on ubuntu)

@myconode
Copy link
Contributor

I'm experiencing this same issue with the latest nvm (0.29.0) - is the protocol to open a new issue?

I have node installed in my system, ran the nvm install script, set the NVM_DIR variable & sourced the nvm shell script in my shell profile.

# /usr/local/bin/node
which node

# v4.2.4
node -v

# /Users/thelostspore/.nvm
echo $NVM_DIR

nvm install 0.12.9
nvm alias default 0.12.9
nvm use default

# /Users/thelostspore/.nvm/versions/node/v0.12.9/bin/node
which node

nvm is installed and behaving normally except that the new shell sessions always revert to the system version of node. Am I missing something?

@ljharb
Copy link
Member

ljharb commented Dec 24, 2015

@thelostspore let's debug here for a moment - can you gist the output of nvm debug, and the contents of your bashrc (or appropriate profile) file?

@myconode
Copy link
Contributor

Thanks @ljharb

Output of nvm debug:

$SHELL: /bin/bash
$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

~/.bash_profile sources from my ~/.bashrc. The relevant nvm portion:

export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh

I think its worth mentioning that I first attempted install via the nvm install script, then a manual install, before finally a homebrew install (uninstalling nvm before each new attempt). Currently nvm is installed via homebrew and everything appears to be working aside from the default node version in new shell sessions.

Also, I placed an .nvmrc with the desired default node version in my home directory. Still no luck.

@ljharb
Copy link
Member

ljharb commented Dec 24, 2015

nvm is entirely unsupported when installed via homebrew - can you try brew uninstalling it, and installing it just with the install script, and then I should be able to help you get everything working?

@myconode
Copy link
Contributor

Ok, uninstalled nvm via homebrew, ran install script, installed node (0.12.9), and set as default.
nvm debug

$SHELL: /bin/bash
$NVM_DIR: '$HOME/.nvm'
$PREFIX: ''
$NPM_CONFIG_PREFIX: ''
nvm current: v0.12.9
which node: $NVM_DIR/versions/node/v0.12.9/bin/node
which iojs: 
which npm: $NVM_DIR/versions/node/v0.12.9/bin/npm
npm config get prefix: $NVM_DIR/versions/node/v0.12.9
npm root -g: $NVM_DIR/versions/node/v0.12.9/lib/node_modules

The nvm command is available in new shells but the system version of node is the default (nvm current).
Thanks again for looking into this.

@ljharb
Copy link
Member

ljharb commented Dec 25, 2015

ok so nvm alias default node works? what does nvm alias print out? when you open a new shell, is the above what nvm debug prints out? Can you confirm what nvm current prints out with the above nvm debug printout?

@myconode
Copy link
Contributor

I don't think the alias command is working as expected.

nvm use default

# 0.12.9
nvm current

# default -> node (-> v4.2.4) .. this should be 0.12.9, according to `nvm alias`
nvm alias default node

nvm alias

default -> node (-> v4.2.4)
node -> 0.12 (-> v0.12.9)
system -> 4.2 (-> v4.2.4)
stable -> 4.2 (-> v4.2.4) (default)
iojs -> N/A (default)

Same results with nvm alias default 0.12. In a new shell nvm current yields system.

@ljharb
Copy link
Member

ljharb commented Dec 25, 2015

aha! @thelostspore try nvm unalias node. you may have ran nvm alias node 0.12 at some point, and I don't think that's a scenario i'd anticipated. also, what's ls -la $NVM_DIR print out?

@myconode
Copy link
Contributor

Ok, removed all aliases (I definitely tried nvm alias node 0.12 ;) ), and then tried setting the default again. nvm current in new shells still yields system.

$NVM_DIR

drwxr-xr-x  20 thelostspore  staff   680B Dec 24 16:23 .
drwxr-xr-x+ 52 thelostspore  staff   1.7K Dec 24 16:20 ..
drwxr-xr-x  13 thelostspore  staff   442B Dec 24 16:20 .git
-rw-r--r--   1 thelostspore  staff    12B Dec 24 16:20 .gitattributes
-rw-r--r--   1 thelostspore  staff   117B Dec 24 16:20 .gitignore
-rw-r--r--   1 thelostspore  staff    14B Dec 24 16:20 .npmignore
-rw-r--r--   1 thelostspore  staff   1.4K Dec 24 16:20 .travis.yml
-rw-r--r--   1 thelostspore  staff   763B Dec 24 16:20 CONTRIBUTING.md
-rw-r--r--   1 thelostspore  staff   1.1K Dec 24 16:20 LICENSE.md
-rw-r--r--   1 thelostspore  staff   5.1K Dec 24 16:20 Makefile
-rw-r--r--   1 thelostspore  staff   8.9K Dec 24 16:20 README.markdown
drwxr-xr-x   3 thelostspore  staff   102B Dec 25 11:08 alias
-rw-r--r--   1 thelostspore  staff   1.8K Dec 24 16:20 bash_completion
drwxr-xr-x   4 thelostspore  staff   136B Dec 25 10:51 bin
-rwxr-xr-x   1 thelostspore  staff   7.5K Dec 24 16:20 install.sh
-rwxr-xr-x   1 thelostspore  staff   304B Dec 24 16:20 nvm-exec
-rwxr-xr-x   1 thelostspore  staff    65K Dec 24 16:20 nvm.sh
-rw-r--r--   1 thelostspore  staff   1.4K Dec 24 16:20 package.json
drwxr-xr-x   9 thelostspore  staff   306B Dec 24 16:20 test
drwxr-xr-x   3 thelostspore  staff   102B Dec 24 16:22 versions

@ljharb
Copy link
Member

ljharb commented Dec 25, 2015

great, and inside $NVM_DIR/alias?

@myconode
Copy link
Contributor

Only default exists, contents are 0.12

@ljharb
Copy link
Member

ljharb commented Dec 25, 2015

ok - so just to confirm once more: new shell, nvm works fine, but nvm current starts out as system, despite the presence of nvm alias default. There's no other aliases. There isn't any .nvmrc file (and adding one doesn't change the behavior), and nvm use 0.12 works fine. What does nvm use with no arguments print out? Also, what about echo $PATH? it's possible that you're setting your system node's PATH after nvm is loaded.

@jordancardwell
Copy link

FWIW

I had to delete .nvm/current, an alias to the version mine was stuck on.

pretty sure this was a side effect of having installed via brew.

I uninstalled via brew and installed via the README... that current alias, however, was still hanging around.. deleting that resolved it.

also kudos @ljharb for being helpful AF

@ljharb
Copy link
Member

ljharb commented Sep 9, 2016

That current alias is from an older version of nvm - by default now, it's not created at all, unless an env var is set.

@wq1308786830
Copy link

image
seems windows nvm have no 'nvm alias default xxx' command, and I still have this problem, want some help

@ljharb
Copy link
Member

ljharb commented Apr 27, 2017

@wq1308786830 windows nvm isn't this project; that's nvm-windows - it's linked from the readme. You're on the wrong repo.

@wq1308786830
Copy link

@ljharb thanks

@omkarsheral1989
Copy link

use
nvm alias default
eg
nvm alias default 8.9.3

@JonahMoses
Copy link

JonahMoses commented Jan 8, 2018

Having similar problems as some other users above and have tried all suggestions given but I'm still seeing some unexpected behavior. I'm on mac os with zsh, installed properly from the readme and most behaviors work as intended, except when I source my zshrc file.

  1. I start a new terminal and it uses 9.3 as my desired default.
  2. I source my zsh file then it reverts to using system node vs default.

screen shot 2018-01-07 at 11 49 44 pm

I should also add that if afterwards I use nvm use default it correctly changes my select to 9.3

Here is whats in my zshrc that seems relevant:
export PATH="~/.yarn/bin:/usr/local/bin:/usr/local/sbin:~/bin:$PATH"
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

@ljharb
Copy link
Member

ljharb commented Jan 8, 2018

The "prefix" error is the issue; do you have an ~/.npmrc file, or any env vars set with "prefix" in their name?

@JonahMoses
Copy link

JonahMoses commented Jan 8, 2018

I do not have a /.npmrc file and do not believe I have any env vars set with "prefix".

Also I'm very impressed with your response time and the previous responses you've given. Thank you!

@ljharb
Copy link
Member

ljharb commented Jan 8, 2018

@JonahMoses would you mind filing a new issue for this one? I assume that at the end of that screenshot, nvm use node would error out with that prefix error?

@JonahMoses
Copy link

@ljharb nvm use node doesn't provide the prefix error but switches to the default node.

I've filed a new issue here: #1703

@anjum121
Copy link

For me when I do nvm alias default 6.11.0 it wasn't persistent, each time when I close iTerm (my shell) it was getting set to4.4.4, which isn't what I had set.

This trick work for me, goto /Users/anjum/.nvm/alias folder open default file and changed 4.4 to 6.11

Thanks

@ljharb
Copy link
Member

ljharb commented Jan 29, 2018

@anjum121 that suggests that the permissions on that file aren't correct; such that nvm alias default whatever is able to change it.

@wouterkroes
Copy link

nvm alias default xxx (https://eric.blog/2016/08/23/set-default-node-version-with-nvm/) does the trick

@marcusjwhelan
Copy link

That was it! I had an alias in my .bashrc setting node='nodejs'. I think I remember needing to put that in there when I first installed nodejs... But why? I guess this has nothing to do with nvm. Thanks!

This is sort of a fix when using Yarn. many people suggest it to get rid of issues with node/nodejs naming problems with packages and using yarn.

@ljharb
Copy link
Member

ljharb commented Mar 5, 2019

The only thing that should ever care about “nodejs” is ubuntu’s broken (for node) default repository; it shouldn’t ever be required.

@aleen42
Copy link

aleen42 commented Jan 16, 2020

How can I run a node script to change environment?

const {execSync} = require('child_process');

execSync('. ~/.nvm/nvm.sh && nvm use v12.9.1', {encoding: 'utf8'});
execSync('npm version', {encoding: 'utf8'}); // => try to use [email protected]

Even if:

execSync('. ~/.nvm/nvm.sh && node build.js'); // cannot use nvm within build.js

But actually it does not work, and how can I do this?

@ljharb
Copy link
Member

ljharb commented Jan 16, 2020

@aleen42 you pretty much can't. nvm is a sourced shell function, so it's only usable within the same shell session you sourced nvm.sh in. In your example, you'd have to combine every v12.9.1 command into the same execSync call.

@mishabruml
Copy link

this didn't work for me on circleci. the only way that I could get it to work was by uninstalling the version of node that the circleci image shipped with. the default alias was then respected by new shells/run steps as expected

@markcollinscope
Copy link

Hi guys, reading through the above, it isn't entirely clear if there was a solution to this, at least for Ubuntu/Linux latest versions.
Anyone have a succint summary?
Thanks for you assistance.

@ljharb
Copy link
Member

ljharb commented Sep 12, 2021

@markcollinscope this specific issue was caused by trying to alias node to “nodejs”. If you have a different problem, please file a new issue and fill out the full issue template.

@aleen42
Copy link

aleen42 commented Sep 13, 2021

How can I run a node script to change the environment?

const {execSync} = require('child_process');

execSync('. ~/.nvm/nvm.sh && nvm use v12.9.1', {encoding: 'utf8'});
execSync('npm version', {encoding: 'utf8'}); // => try to use [email protected]

Even if:

execSync('. ~/.nvm/nvm.sh && node build.js'); // cannot use nvm within build.js

But actually it does not work, and how can I do this?

Finally, we have used a standalone docker image to build up CI and using environment parameters to specify the node version with nvm when starting up a pipeline.

@LuuchoRocha
Copy link

@markcollinscope How are you loading nvm into your shell?
In my case, I did something wrong which was adding the lines:

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

to my .profile, which caused the $PATH to be pointing to the old folders, even after replacing the aliases with nvm alias
Anyways, closing the user session and reopening did the job.
And after I've removed those lines from the .profile and added into my .zshrc

@markcollinscope
Copy link

@LuuchoRocha - thank you for your input. I will check this out and get back to you to let you know. Appreciated!

@baughmann
Copy link

baughmann commented Apr 15, 2023

@LuuchoRocha

Unfortunately, this seems broken, at least on linux:

My aliases:

nvm ls
       v16.20.0
->     v18.16.0
default -> 16.20.0 (-> v16.20.0)
node -> default (-> v16.20.0)
stable -> default (-> v16.20.0)
iojs -> N/A (default)
unstable -> N/A (default)
lts/* -> lts/hydrogen (-> v18.16.0)
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.21.3 (-> N/A)
lts/gallium -> v16.20.0
lts/hydrogen -> v18.16.0

You will notice that 18 is active even though both the node and default are set to 16. Of course, this means node --version is 16.20.0.

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

Linux Kernel:

uname -r
6.2.10-300.fc38.x86_64

In order for my preference for 16 to persist across session, I had to nvm use default (because the active was still 18 for some reason) followed by a nvm uninstall 18

@ljharb
Copy link
Member

ljharb commented Apr 15, 2023

@baughmann what version of nvm do you have? node isn't supposed to point at default, so you may need to run nvm unalias node.

@baughmann
Copy link

baughmann commented Apr 15, 2023

@ljharb I pointed node to default manually in an attempt to get it to work when simply aliasing default did not. I'm using nvm version 0.39.3, apologies for not mentioning that

@codytodonnell
Copy link

In case this helps anyone else: I was struggling with this and could not get nvm alias default node or nvm alias default 20 to work. New shells would always use v18.16.1. I had no system node and which node and $PATH all looked right.

After doing a simple nvm uninstall 18, I can successfully change the default version used in new terminals.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests