nvm keeps "forgetting" node in new terminal session - node.js

Upon using a new terminal session in OS X, nvm forgets the node version and defaults to nothing:
$ nvm ls:
.nvm
v0.11.12
v0.11.13
I have to keep hitting nvm use v.0.11.13 in every session:
.nvm
v0.11.12
-> v0.11.13
I've tried both the brew install, as well as the official installation script.
My .profile for the brew version:
#nvm
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
And for the install.sh script:
$ curl https://raw.githubusercontent.com/creationix/nvm/v0.10.0/install.sh | bash
#nvm
export NVM_DIR="/Users/farhad/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
Any clue to what I'm doing wrong?

Try nvm alias default. For example:
$ nvm alias default 0.12.7
This sets the default node version in your shell. Then verify that the change persists by closing the shell window, opening a new one, then:
node --version

Alias to node itself to avoid updating the default alias along with node version updates later on.
nvm alias default node

In my case, another program had added PATH changes to .bashrc
If the other program changed the PATH after nvm's initialisation, then nvm's PATH changes would be forgotten, and we would get the system node on our PATH (or no node).
The solution was to move the nvm setup to the bottom of .bashrc
### BAD .bashrc ###
# NVM initialisation
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
# Some other program adding to the PATH:
export PATH="$ANT_ROOT:$PATH"
Solution:
### GOOD .bashrc ###
# Some other program adding to the PATH:
export PATH="$ANT_ROOT:$PATH"
# NVM initialisation
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
(This was with bash 4.2.46 on CentOS. It seems to me like a bug in bash, but I may be mistaken.)

To install the latest stable version:
nvm install stable
To set default to the stable version (instead of a specific version):
nvm alias default stable
To list installed versions:
nvm list
As of v6.2.0, it will look something like:
$ nvm list
v4.4.2
-> v6.2.0
default -> stable (-> v6.2.0)
node -> stable (-> v6.2.0) (default)
stable -> 6.2 (-> v6.2.0) (default)
iojs -> N/A (default)

nvm does its job by changing the PATH variable, so you need to make sure you aren't somehow changing your PATH to something else after sourcing the nvm.sh script.
In my case, nvm.sh was being called in .bashrc but then the PATH variable was getting updated in .bash_profile which caused my session to find the system node before the nvm node.

Here is a simple instruction:
1) Install:
nvm install 8.10.0
2) Use once per terminal
nvm use 8.10.0
3) Set up as default for all terminals
nvm alias default 8.10.0
You may need to use root permissions to perform those actions.
And don't forget to check nvm documentation for more info.
Also note that you may need to specify node version for your IDE:

None of these solutions worked in my environment, nvm always seems to load the first installed version of node no matter what (unless you change it temporarily via nvm use).
The only way to change the default I have found is to:
Clear nvm cache: nvm cache clear
Set default to desired version: nvm alias default 12 (or whatever version)
Switch to desired version: nvm use 12
Uninstall all other versions:
nvm ls (to list installations)
nvm uninstall x (run for each installation that is not the default)
Reinstall other versions: nvm install x
You can use this script to automate this process (just change the first variable to your desired version) - it will re-install all versions you had previously automatically.
DEFAULT_NVM_VERSION=16
nvm cache clear
nvm install $DEFAULT_NVM_VERSION
nvm alias default $DEFAULT_NVM_VERSION
NVERS=$(nvm ls --no-alias | grep -v -- "->" | grep -o "v[0-9.]*")
while read ver; do nvm uninstall $ver; done <<< $NVERS
while read ver; do nvm install $ver; done <<< $NVERS
nvm use $DEFAULT_NVM_VERSION
Or as a one-liner:
DEFAULT_NVM_VERSION=16 && nvm cache clear && nvm install $DEFAULT_NVM_VERSION && nvm alias default $DEFAULT_NVM_VERSION && NVERS=$(nvm ls --no-alias | grep -v -- "->" | grep -o "v[0-9.]*") && while read ver; do nvm uninstall $ver; done <<< $NVERS && while read ver; do nvm install $ver; done <<< $NVERS && nvm use $DEFAULT_NVM_VERSION
New terminals should now respect the default version.

The top rated solutions didn't seem to work for me. My solution is below:
Uninstall nvm completely using homebrew:brew uninstall nvm
Reinstall brew install nvm
In Terminal, follow the steps
below(these are also listed when installing nvm via homebrew):
mkdir ~/.nvm
cp $(brew --prefix nvm)/nvm-exec ~/.nvm/
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
The steps outlined above will add NVM's working directory to your $HOME path, copy nvm-exec to NVM's working directory and add to $HOME/.bashrc, $HOME/.zshrc, or your shell's equivalent configuration file.(again taken from whats listed on an NVM install using homebrew)

I'm using ZSH so I had to modify ~/.zshrc with the lines concerning NVM in that order:
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
source ~/.nvm/nvm.sh

If you have tried everything still no luck you can try this :_
1 -> Uninstall NVM
rm -rf ~/.nvm
2 -> Remove npm dependencies by following this
3 -> Install NVM
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash
4 -> Set ~/.bash_profile configuration
Run sudo nano ~/.bash_profile
Copy and paste following this
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
5 -> CONTROL + X save the changes
6 -> Run . ~/.bash_profile
7 -> Now you should have nvm installed on your machine, to install node run nvm install v7.8.0 this will be default node version or you can install any version of node

This question has mentioned for the OSX, but it happened to me in my linux OS.
I tried using nvm alias default <version> but for each new terminal session the used node version was forgotten.
so, here is the solution that i figured out.
make sure to set a default alias for node version,put the following code in .bashrc, and source .bashrc.
export NVM_DIR="/home/bonnie/.nvm"
## If the file exists and is not empty
if [ -s "$NVM_DIR/nvm.sh" ]; then
## Source it
source "$NVM_DIR/nvm.sh"
fi
NODE_DEFAULT_VERSION=$(<"$NVM_DIR/alias/default")
export PATH="$NVM_DIR/versions/node/$NODE_DEFAULT_VERSION/bin":$PATH
descriptive solution link

Doing nvm install 10.14, for example, will nvm use that version for the current shell session but it will not always set it as the default for future sessions as you would expect. The node version you get in a new shell session is determined by nvm alias default. Confusingly, nvm install will only set the default alias if it is not already set. To get the expected behaviour, do this:
nvm alias default ''; nvm install 10.14
This will ensure that that version is downloaded, use it for the current session and set it as the default for future sessions.

run this after you installed any version,
n=$(which node);n=${n%/bin/node}; chmod -R 755 $n/bin/*; sudo cp -r $n/{bin,lib,share} /usr/local
This command is copying whatever version of node you have active via nvm into the /usr/local/ directory and setting the permissions so that all users can access them.

I have found a new way here. Using n Interactively Manage Your Node.js helps.

I was facing the same issue while using the integrated terminal in VS Code editor. Restarting VS Code after changing the node version using nvm fixed the issue for me.

I use NVM with zsh bash
follow this link to remove nvm
$ brew install nvm
I ran this line $ source $(brew --prefix nvm)/nvm.sh after installation and restart terminal
place the line bellow in ~/.zshrc file, also instructed by nvm official GitHub page
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
# place this after nvm initialization!
autoload -U add-zsh-hook
load-nvmrc() {
local node_version="$(nvm version)"
local nvmrc_path="$(nvm_find_nvmrc)"
if [ -n "$nvmrc_path" ]; then
local nvmrc_node_version=$(nvm version "$(cat "${nvmrc_path}")")
if [ "$nvmrc_node_version" = "N/A" ]; then
nvm install
elif [ "$nvmrc_node_version" != "$node_version" ]; then
nvm use
fi
elif [ "$node_version" != "$(nvm version default)" ]; then
echo "Reverting to nvm default version"
nvm use default
fi
}
add-zsh-hook chpwd load-nvmrc
load-nvmrc

For some reason in my .bashrc file I found this:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use # This loads nvm
and had to remove --no-use flag, which I don't remember putting there in a first place...
Just another thing to check.

If you also have SDKMAN...
Somehow SDKMAN was conflicting with my NVM. If you're at your wits end with this and still can't figure it out, I just fixed it by ignoring the "THIS MUST BE AT THE END OF THE FILE..." from SDKMAN and putting the NVM lines after it.
#THIS MUST BE AT THE END OF THE FILE FOR SDKMAN TO WORK!!!
export SDKMAN_DIR="/Users/myname/.sdkman"
[[ -s "/Users/myname/.sdkman/bin/sdkman-init.sh" ]] && source "/Users/myname/.sdkman/bin/sdkman-init.sh"
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

On my end, I had to change both aliases STABLE and DEFAULT
nvm alias stable {node_version}
nvm alias default {node_version}

$ nvm alias default {NODE_VERSION}
when we use the above command, only update the node version but the npm still uses the old version.
Here is another solution for update the both node and npm, in my case i want to use node 8.9.4 and i have used the below command.
$ nvm use default 8.9.4
And the command returns the output.
Now using node v8.9.4 (npm v5.6.0)

As mentioned in the repository's issues section, nvm use is just for a lifetime of the shell. I have found this very useful, but sometimes it may put you in trouble actually when you are working on different codebases which need different versions of code.
This is the link for the related discussion in GitHub

For some reason, in my ~/.profile file I found that was selecting the version of node, overriding the alias default command of nvm
Just another thing to check.

Linux/ubuntu
how to solve this you can see here
https://i.ibb.co/vQpMrpb/2022-11-19-18-14.jpg
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 /.
https://github.com/nvm-sh/nvm/issues/658

Also in case you had node installed before nvm check in your ~/.bash_profile to not have something like :
export PATH=/bin:/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin:$PATH
If you do have it, comment/remove it and nvm should start handling the default node version.

On Ubuntu there is a potential issue if you are running a non-interactive shell, for example from a cronjob, or an init or upstart script.
Ubuntu's default .bashrc has this guard at the top which drops out early if the shell is non-interactive:
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
So in order to get NVM loaded for non-interactive scripts, you can edit ~/.bashrc and move NVM's init to the top of the file, just above the guard.
Even safer is to copy the NVM init so it appears twice. That will address the concern mentioned in other answers, when other lines are modifying the PATH after NVM. NVM doesn't seem to mind being run twice, although it does slow down the shell startup a little.

In the nvm autoload script from the github I had to chage
local node_version="$(nvm version)" to local node_version="$(node -v)"
There was a local install of nvm on my system in my path so nvm version kept saying system no matter what

1.- Install via homebrew
2.- Because I am using zsh terminal, in ~/.zshrc add this lines, if you are using bash you will need to put that lines in ~/.bash_profile
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh

Ref: The issue that is causing this is related to the fact that the folder NPM is trying to access/write to is not owned by the user that is executing the command
The run the commands stated below and You should be good to go!
sudo chown -R `whoami` ~/.npm
sudo chown -R `whoami` /usr/local/lib/node_modules
Try running npm install -g ... or npm uninstall -g ... without sudo and it should run without errors.

Related

NVM Install Error: Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.

I JUST wiped and reformatted my entire Macbook Retina drive to start from scratch,
installed homebrew,
installed xcode and accepted terms and conditions,
went to install nvm with curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.11/install.sh | bash and got the following error:
=> nvm is already installed in /Users/dillon/.nvm, trying to update using git
=> => Compressing and cleaning up git repository
=> Profile not found. Tried ~/.bashrc, ~/.bash_profile, ~/.zshrc, and ~/.profile.
=> Create one of them and run this script again
OR
=> Append the following lines to the correct file yourself:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
=> Close and reopen your terminal to start using nvm or run the following to use it now:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
echo $PATH returns /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin if that's any help.
I tried opening an issue on github but haven't received a response
Any idea what I can do to get nvm and node properly running?
Didn't read this from the nvm install instructions. It fixed my problem.
Note: On OS X, if you get nvm: command not found after running the
install script, one of the following might be the reason:-
your system may not have a [.bash_profile file] where the command is set up. Simply create one with touch ~/.bash_profile and run the
install script again
you might need to restart your terminal instance. Try opening a new tab/window in your terminal and retry.
The following command solved my problem:
sudo chown -R $USER:$(id -gn $USER) /Users/david/.config
Note: Change 'david' in the path for your user.

Uninstalling the currently active version of node.js with nvm

On Ubuntu 16.04, I mistakenly used root to install nvm, and then to install node.js 8.8.1 via nvm. I also used nvm alias default 8.8.1, thinking it would correct my error.
Now I would like to:
Remove the default alias
Uninstall node 8.8.1
Uninstall npm 4.8.5, which came along with node
Uninstall nvm
Reinstall everything correctly for the right non-sudo user
It looks like I've succeeded with the first part:
# nvm unalias default
Deleted alias default - restore it with `nvm alias "default" "8.8.1"`
But nvm refuses to uninstall node 8.8.1, because it is the only version installed:
# nvm uninstall 8.8.1
nvm: Cannot uninstall currently-active node version, v8.8.1 (inferred from 8.8.1).
I am guessing that I first need to disactivate node 8.8.1, but I see nothing in the output of nvm --help which would appear to do this.
What steps do I need to take to completely remove node.js, npm and nvm from the machine before re-installing everything correctly?
First type
$ nvm deactivate
Then type
$ nvm uninstall 8.8.1
rm -Rf ~/.nvm
This is the nuclear option in my case. just -R would ask me if I really wanted to delete every file in the nvm folder.
You can uninstall the nodejs by using the following command.
yum remove nodejs
However, this will not remove the nvm from your linux box. To remove that try the below command.
nvm unload
nvm deactivate
can temporarily deactivate the nvm'ed node.
You can comment out the path in .zshrc etc, for the next session.
#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

Can't use nvm from bash script

I am trying to write a shell script to automate my dev environment set-up (install python, nvm, node, mongo etc...). I am using nvm to install Node. It tells you to close and reopen your terminal to start using the nmv command. I tried to source .bashrc and .profile to make the command available right away so I can continue running the script with nvm install, but it doesn't work.
Here is the segment of my script related to installing NVM / Node:
#install nvm and latest node version
# sourcing profile and bashrc is not working here. nvm does not execute the next two lines to install node.
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
source ~/.profile
source ~/.bashrc
nvm install 5.0
nvm alias default node
I get these messages, but please note that I've already run the script and NVM / Node are already installed and working. I can also use nvm and node in the same terminal I run the script from after it completes. It just doesn't work in the script.
=> Downloading nvm from git to '/home/myDir/.nvm'
=> fatal: destination path '/home/myDir/.nvm' already exists and is not an empty directory.
fatal: Not a git repository (or any of the parent directories): .git
fatal: Not a git repository (or any of the parent directories): .git
=> Source string already in /home/myDir/.bashrc
=> Close and reopen your terminal to start using nvm
./install-programs.sh: line 27: nvm: command not found
./install-programs.sh: line 28: nvm: command not found
if you have nvm running on the main shell, you just need to add:
export NVM_DIR=$HOME/.nvm;
source $NVM_DIR/nvm.sh;
in your script
Here is what worked for me.
First install nvm (once and separately) using SSH or console:
$ wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.30.2/install.sh | bash
Then in your script, loading your profile as follows:
. ~/.nvm/nvm.sh
. ~/.profile
. ~/.bashrc
And with some luck nvm should become available inside the script.
nvm install 4.4.2
Tada!
Just put this on top of your script:
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
Worked like a charm here.
This script works fine for me:
#!/usr/bin/env bash
if [ ! -d ~/.nvm ]; then
curl https://raw.githubusercontent.com/creationix/nvm/v0.11.1/install.sh | bash
source ~/.nvm/nvm.sh
source ~/.profile
source ~/.bashrc
nvm install 5.0
npm install
npm run front
fi
Nowadays, you can simply do this:
env NODE_VERSION=<dd> /home/<user>/.nvm/nvm-exec npm run front
Simply sourcing nvm.sh didn't work for me (from a systemd .service file), the PATH didn't include ~/.nvm...
Credit where credit is due: https://gist.github.com/joepie91/73ce30dd258296bd24af23e9c5f761aa#gistcomment-2215867

Troubleshoot installing NVM.

I am following these instructions:
https://keymetrics.io/2015/02/03/installing-node-js-and-io-js-with-nvm/
and running this curl request:
curl https://raw.githubusercontent.com/creationix/nvm/v0.23.2/install.sh | bash
It opens my bash_profile (not my bashrc?) and nothing changes. I then do not have the npm command available to me in my bash and there is no .nvm file in my home directory and node -v doesn't work. Any ideas on what might be going wrong?
The NVM documentation says that the installation script will try to add the source line to your ~/.bash_profile:
The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc or ~/.profile).
And probably you are loading your ~/.bashrc when opening your terminal without a reference to the ~/.bash_profile so you are not 'sourcing' the nvm script.
Try adding these lines to your ~/.bashrc and restart your terminal:
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
(I'm assuming that ~/.nvm is your NVM installation directory).
Extra: You can check this link to learn about the difference between .bashrc and _bash_profile

NVM: command not found but present in .bashrc

I'm trying to install nvm on my Mac OS (according to https://github.com/creationix/nvm#installation)
I'm running
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.27.1/install.sh | bash
I see that my .bashrc contains
export NVM_DIR="/Users/yanamikhaylenko/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
However when I run
nvm install node && nvm alias default node
I receive
-bash: nvm: command not found
Should I export path in .profile or do something else?
Please, note that I'm using Node.js v 4.1.1

Resources