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 · 56 comments
Closed

"nvm use" not persisting #658

YourDeveloperFriend opened this issue Feb 12, 2015 · 56 comments

Comments

@YourDeveloperFriend
Copy link

@YourDeveloperFriend YourDeveloperFriend commented Feb 12, 2015

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 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 Feb 12, 2015
@YourDeveloperFriend
Copy link
Author

@YourDeveloperFriend YourDeveloperFriend commented Feb 12, 2015

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

@YourDeveloperFriend YourDeveloperFriend commented Feb 13, 2015

$ 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

@YourDeveloperFriend YourDeveloperFriend commented Feb 13, 2015

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

@YourDeveloperFriend YourDeveloperFriend commented Feb 13, 2015

$ 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 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

@YourDeveloperFriend YourDeveloperFriend commented Feb 13, 2015

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 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 Feb 13, 2015
@YourDeveloperFriend
Copy link
Author

@YourDeveloperFriend YourDeveloperFriend commented Feb 13, 2015

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

@fergiemcdowall fergiemcdowall commented Oct 1, 2015

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

@myconode myconode commented Dec 24, 2015

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

@myconode myconode commented Dec 24, 2015

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

@myconode myconode commented Dec 25, 2015

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

@myconode myconode commented Dec 25, 2015

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

@myconode myconode commented Dec 25, 2015

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 ljharb commented Dec 25, 2015

great, and inside $NVM_DIR/alias?

@myconode
Copy link
Contributor

@myconode myconode commented Dec 25, 2015

Only default exists, contents are 0.12

@ljharb
Copy link
Member

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

@myconode
Copy link
Contributor

@myconode myconode commented Dec 25, 2015

👍 Thanks again @ljharb

@g13013
Copy link

@g13013 g13013 commented Jan 13, 2016

for those who have similar problem, forcing npm prefix config also causes this issue!

@ljharb
Copy link
Member

@ljharb ljharb commented Jan 13, 2016

The "prefix" config is not supported by nvm, so please don't use it.

@cornelius-k
Copy link

@cornelius-k cornelius-k commented Jul 8, 2016

nvm alias default 6.3.0 resolved my issue, version number being the last argument

@ljharb
Copy link
Member

@ljharb ljharb commented Jul 9, 2016

nvm alias default node works as well.

@rocco
Copy link

@rocco rocco commented Aug 26, 2016

haven't seen such a positive and constructive issue thread in a while - good job @ljharb

@jordancardwell
Copy link

@jordancardwell jordancardwell commented Sep 9, 2016

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

@wq1308786830 wq1308786830 commented Apr 27, 2017

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

@wq1308786830 wq1308786830 commented Apr 27, 2017

@ljharb thanks

@omkarsheral1989
Copy link

@omkarsheral1989 omkarsheral1989 commented Dec 15, 2017

use
nvm alias default
eg
nvm alias default 8.9.3

@JonahMoses
Copy link

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

@JonahMoses JonahMoses commented Jan 8, 2018

@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

@anjum121 anjum121 commented Jan 29, 2018

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

@wouterkroes wouterkroes commented Feb 13, 2019

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

@marcusjwhelan
Copy link

@marcusjwhelan marcusjwhelan commented Mar 4, 2019

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 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 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 node@12.9.1

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

@mishabruml mishabruml commented Apr 22, 2021

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

@markcollinscope markcollinscope commented Sep 12, 2021

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 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 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 node@12.9.1

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.

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

Successfully merging a pull request may close this issue.

None yet