Set node version with NVM or install if not available. - node.js

I'm trying to add to my bash profile something that will set my node version to a specific version, and if the node version is not installed then install it. What I have so far is:
. /usr/local/opt/nvm/nvm.sh
if [[ $(nvm use v6.9.1) == "" ]]; then
nvm install v6.9.1
fi
However, the problem is that the $(nvm use v6.9.1) is run in a subshell and my node version doesn't get switched.
a) Is there any way to have $(nvm use v6.9.1) run in the current shell?
b) Is there a better way of doing this?
Previously I was just running nvm install v6.9.1 but this was kinda slow which was an issue as it runs each time I open a new terminal.
Thanks Matt!

I have a bash alias I use for this that works for multiple versions:
alias nvmuse='nvm use || nvm install $(cat .nvmrc)'

Have you tried grepping nvm ls?
. /usr/local/opt/nvm/nvm.sh
if [[ $(nvm ls | grep v6.9.1) == "" ]]; then
nvm install v6.9.1
else
nvm use v6.9.1
fi
Is it any faster than using nvm install v6.9.1 for you?
EDIT: You can also set a default version that will always be loaded by default. You can do it by running nvm alias default 6.9.1.
You can try changing your script to this:
if [[ $(node -v) != "v6.9.5" ]]; then
nvm install v6.9.5
nvm alias default v6.9.5
fi
It will take a little long, but just for the first time

In the current version of nvm, nvm install does not reinstall node if it's already installed.
Examples:
$ nvm install v16.0.4
v16.14.2 is already installed.
# The same if you have put the version in your project's .nvmrc
$ nvm install
v16.0.4 is already installed.
Note however, if you specify an ambiguous version such as v16 and a newer version of v16 is available, then nvm will download and install the newer version, ignoring your older version of v16.
$ node --version
v16.0.4
$ nvm install v16
Downloading and installing node v16.14.2...
So specifying v16 is good if you always want to be up-to-date, and have the latest security patches. But eventually you might end up with lots of versions of node installed!
To keep just one version installed (to save disk space, or to keep packages you previously installed globally with npm) then specify the full version v16.0.4.

This works. If use failed, it will execute install.
#!/bin/sh
nvm use 14.18.1 || nvm install 14.18.1
# or if you don't need the warning
nvm use 14.18.1 2>/dev/null || nvm install 14.18.1

Related

Grepping for NVM version doesn't seem to work to detect my version and then install it [duplicate]

I'm trying to add to my bash profile something that will set my node version to a specific version, and if the node version is not installed then install it. What I have so far is:
. /usr/local/opt/nvm/nvm.sh
if [[ $(nvm use v6.9.1) == "" ]]; then
nvm install v6.9.1
fi
However, the problem is that the $(nvm use v6.9.1) is run in a subshell and my node version doesn't get switched.
a) Is there any way to have $(nvm use v6.9.1) run in the current shell?
b) Is there a better way of doing this?
Previously I was just running nvm install v6.9.1 but this was kinda slow which was an issue as it runs each time I open a new terminal.
Thanks Matt!
I have a bash alias I use for this that works for multiple versions:
alias nvmuse='nvm use || nvm install $(cat .nvmrc)'
Have you tried grepping nvm ls?
. /usr/local/opt/nvm/nvm.sh
if [[ $(nvm ls | grep v6.9.1) == "" ]]; then
nvm install v6.9.1
else
nvm use v6.9.1
fi
Is it any faster than using nvm install v6.9.1 for you?
EDIT: You can also set a default version that will always be loaded by default. You can do it by running nvm alias default 6.9.1.
You can try changing your script to this:
if [[ $(node -v) != "v6.9.5" ]]; then
nvm install v6.9.5
nvm alias default v6.9.5
fi
It will take a little long, but just for the first time
In the current version of nvm, nvm install does not reinstall node if it's already installed.
Examples:
$ nvm install v16.0.4
v16.14.2 is already installed.
# The same if you have put the version in your project's .nvmrc
$ nvm install
v16.0.4 is already installed.
Note however, if you specify an ambiguous version such as v16 and a newer version of v16 is available, then nvm will download and install the newer version, ignoring your older version of v16.
$ node --version
v16.0.4
$ nvm install v16
Downloading and installing node v16.14.2...
So specifying v16 is good if you always want to be up-to-date, and have the latest security patches. But eventually you might end up with lots of versions of node installed!
To keep just one version installed (to save disk space, or to keep packages you previously installed globally with npm) then specify the full version v16.0.4.
This works. If use failed, it will execute install.
#!/bin/sh
nvm use 14.18.1 || nvm install 14.18.1
# or if you don't need the warning
nvm use 14.18.1 2>/dev/null || nvm install 14.18.1

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

Node.js installation using nvm

I am trying to install node.js on mac running macOS Sierra using nvm but after installing nvm when I try to run
nvm install node it says Version 'node' not found - try nvm ls-remote to browse available versions.
when I run nvm ls-remote I only get list of io.js and not node.
does nvm now support the node installation or just only io.js?
Please help me figure it out or should I just go with the standard package installer.
I am suspecting you don't have the nvm script in your startup files. Hope this helps.
First make sure you have a base Node.js version installed.
brew install node
This will get you the latest stable Node.js version. You can now proceed to installing NVM using either curl or wget. Also remember to add the scripts to your ~/.zshrc file assuming you're using zsh
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
To install Node.js version after successfully setting up NVM
nvm install v6.9.1
If all fails you can also use N: Node Version Manager

How to properly upgrade node using nvm

Is it possible to upgrade node right in place, instead of manually installing the latest stable version?
I have installed node.js version 5.0 with nvm, but now I want to update it to 5.4. I'm trying to avoid having to manually reinstall all of my global packages (e.g. by running npm install -g grunt-cli bower yo yoman-angular-generator blabla blablablabla...).
This may work:
nvm install NEW_VERSION --reinstall-packages-from=OLD_VERSION
For example:
nvm install 6.7 --reinstall-packages-from=6.4
then, if you want, you can delete your previous version with:
nvm uninstall OLD_VERSION
Where, in your case,
NEW_VERSION = 5.4
OLD_VERSION = 5.0
Alternatively, try:
nvm install stable --reinstall-packages-from=current
You can more simply run one of the following commands:
Latest version:
nvm install node --reinstall-packages-from=node
Stable (LTS) version: (if currently in use)
nvm install "lts/*" --reinstall-packages-from="$(nvm current)"
This will install the appropriate version and reinstall all packages from the currently used node version.
This saves you from manually handling the specific versions.
Kudos to #m4js7er for commenting about the LTS version.
⚡ TWO Simple Solutions:
To install the latest version of node and reinstall the old version packages just run the following command.
nvm install node --reinstall-packages-from=node
To install the latest lts (long term support) version of node and reinstall the old version packages just run the following command.
nvm install --lts /* --reinstall-packages-from=node
Here's a GIF animation to support this answer:
if you have 4.2 and want to install 5.0.0 then
nvm install v5.0.0 --reinstall-packages-from=4.2
the answer of gabrielperales is right except that he missed the "=" sign at the end. if you don't put the "=" sign then new node version will be installed but the packages won't be installed.
source: sitepoint
Here are the steps that worked for me for Ubuntu OS and using nvm
Go to nodejs website and get the last LTS version (for example the version will be: x.y.z)
nvm install x.y.z
# In my case current version is: 14.15.4 (and had 14.15.3)
After that, execute nvm list and you will get list of node versions installed by nvm.
Now you need to switch to the default last installed one by executing:
nvm alias default x.y.z
List again or run nvm --version to check:
Update: sometimes even if i go over the steps above it doesn't work, so what i did was removing the symbolic links in /usr/local/bin
cd /usr/local/bin
sudo rm node npm npx
And relink:
sudo ln -s $(which node) /usr/local/bin/nodesudo && ln -s $(which npm) /usr/local/bin/npmsudo && ln -s $(which npx) /usr/local/bin/npx
Node.JS to install a new version.
Step 1 : NVM Install
npm i -g nvm
Step 2 : NODE Newest version install
nvm install *.*.*(NodeVersion)
Step 3 : Selected Node Version
nvm use *.*.*(NodeVersion)
Finish
Bash alias for updating current active version:
alias nodeupdate='nvm install $(nvm current | sed -rn "s/v([[:digit:]]+).*/\1/p") --reinstall-packages-from=$(nvm current)'
The part sed -rn "s/v([[:digit:]]+).*/\1/p" transforms output from nvm current so that only a major version of node is returned, i.e.: v13.5.0 -> 13.
For Windows 11 this worked for me on cmd, used with admin rights:
Prerequisite, in case you just installed NVM, is to open a new cmd window after nvm installation.
See installation instructions here: https://github.com/coreybutler/nvm-windows
Get installed versions, using
nvm list
Get current version
nvm current
Install latest version
nvm install latest
Check installed versions to see for newer version, again using
nvm list
Set current version to the latest (cmd with admin rights), you just installed in the previous step
nvm use PUT_VERSION_NUMBER_TO_BE_USED
You can check again if the change was successful using
nvm list
Remove old version, if no longer needed
nvm remove PUT_VERSION_NUMBER_TO_BE_REMOVED
If you want to use the LTS version, install using
nvm install lts
Here's the steps to upgrade NodeJs version:
Run nvm install node (will install latest version). Alternatively, you
can specify a specific version by running nvm install <node_version>.
Run nvm use <node_version> to use it.
If you want to make it the default version on your machine, run nvm alias default <node_version>.
Additional notes:
To find out what node versions you have on your machine and which one is set as your default one, use nvm list command.

Installing nvm on Ubuntu 14.04

I'm trying to install nvm on Ubuntu 14.04 but it doesn't seem to use the version I specify. I installed following the tutorial here https://github.com/creationix/nvm and I've also tried the one here https://www.digitalocean.com/community/tutorials/how-to-install-node-js-with-nvm-node-version-manager-on-a-vps.
There are 2 node installations on my system already.
which node # => /usr/local/bin/node
node --version # => v0.11.13-pre
which nodejs # => /usr/bin/nodejs
nodejs --version # => v0.10.26
When I install nvm using the curl one liner they give you, and then use
nvm install 0.10.32
It creates an empty folder inside .nvm/v0.10.32 and .nvm/current symlinks to it.
In addition the bin folder is also empty. This problem occurs if I install other
versions of node. I suppose I could just clone a version of node into the folder
its supposed to go in but idk if that's all I have to do. In addition, I'm not sure
I know how to make my system use the nvm current (symlink from /usr/local/bin/node to .nvm/current ?) Without doing anything myself and only following the tutorial, node --version and nodejs --version never uses the version I specify with.
nvm use 0.10.32
Here is my personal guide how to install nvm (node version manager).
reference: https://github.com/creationix/nvm
1.) Install nvm (NOTE: There might be new versions in their website.)
curl https://raw.githubusercontent.com/creationix/nvm/v0.24.0/install.sh | bash
2.) Close the current terminal and use a new terminal then try nvm again.
3.) Install current node.js version by using:
nvm install node_version
ex: nvm install 0.10.32
4.) to check the nodejs version--> nvm list
5.) Setup a default node version in nvm so that everytime you restart
the system it loads a default node version:
to get help: nvm -h
to get list of install node.js versions: nvm ls
to set a default alias: nvm alias default node_version
6.) Uninstall a specific node.js version:
nvm uninstall node_version
I understand that my answer may be outdated, but there was a very nice thesis in the ticket #809, that using
$ npm install -g nvm
is wrong. To fix, you want to do
$ npm uninstall -g nvm
$ apt install curl
$ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.31.1/install.sh | bash
$ source ~/.bashrc
$ nvm install 5.1
$ nvm use 5.1
instead (given that curl hasn't yet been installed on that machine).
I just removed all of the offending node installations until my system used a version that was 0.10.* . It appears I tried to install node before or another program installed it.

Resources