why restarting reverts the version of node - node.js

I've installed node version 5.8 and used
nvm alias default 5.8
to make it default version of node. then I changed the current version of node using
nvm use 5.8
Why every time I turn off the system, default version of node becomes v0.10.30?
More context:
I need to use
. ~/.nvm/nvm.sh
every time too. otherwise nvm is not recognized.

The issue occurs because nvm is not loaded with each new terminal session. As a result, the default Node version is not persisted.
You can load nvm for each terminal session by adding these lines to your .bashrc file:
export NVM_DIR="/Users/gnerkus/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm

Related

Problem choosing node version in Terminal - MacOS Ventura

Whenever I start a new session in iTerm or the Terminal in MacOS Ventura, I get the system label on the right.
A problem I'm facing is that whenever I want to change the node version, it cannot be done. It's always tied to the system version.
This is when I run nvm list. For default it's the right value: 16.18.1. But when I run nvm use default, and run nvm list again, I still see system as the selected version.
Does anyone know why is still using the system one, instead of the default?
I have no idea about the terminal prompt that appears on the right side.
I had the same issue, and I just added the below lines to the .zshrc file:
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 use the latest version or the version that I installed on my machine, I tried the following:
nvm use <node_version>
I hope this helps!

How can I call "nvm use" in a bash script, and have the selected node version retained after the script executes?

For a build process that requires updating from Node.js 12 to 14, I'd like a bash script to detect whether nvm is installed, and, if so, do nvm use v14 (or nvm install v14 if necessary), and then I want the nvm-selected node version to stick at 14 after the bash script terminates, not just for the duration of the script.
I can switch to v14 with this script, but after the script has terminated, the shell environment remains at v12:
#!/bin/bash
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
source ~/.bashrc
nvm --version
echo $NVM_BIN
node --version
nvm use v14
echo $NVM_BIN
node --version
Just executing the nvm command in a bash script is a pain because nvm isn't a true command, but a shell function, and the script has to use the first three lines to set up nvm for the script.
The output is:
0.33.11
/home/pi/.nvm/versions/node/v12.21.0/bin
v12.21.0
Now using node v14.16.0 (npm v6.14.11)
/home/pi/.nvm/versions/node/v14.16.0/bin
v14.16.0
When the script is finished, however:
I think the trick might be making sure the environment variable NVM_BIN persists at the v14 path when the script exits, but I don't know how to do that. In fact, I think it's generally not allowed for the shell in which a script executes to change environment variables in the parent shell.
The nvm commmand, however, is itself a shell script, and whatever it does is persistent after it's done executing. There should be some way for me to make this happen too.
I think if you set an alias with nvm for default,
nvm alias default 14.0.0
the node version will persist across any new terminal instances.
To have the selected node version after the script executes you can run it like this:
. change-node-version.sh
script
node --version
nvm use 14.18.0
node --version
then:
node --version
➜ v14.18.0

By restarting system terminal does not recognize npm and node commands

I have installed node and npm by nvm; At the end of instaling nvm, below message was shown:
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 [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
So I run that command and use nvm, install node versions and npm.
But when I restart my system, I have to run above command otherwise terminal says command not found.
What is the problem and how can I fix it?
The nvm installation should have added a couple of lines to your shell initialization script that makes sure to initialize nvm in every new shell, but for some reason this seems to have not happened.
Assuming that you're using bash, try adding these lines to your ~/.bash_profile
export NVM_DIR="$HOME/.nvm"
. "$(brew --prefix nvm)/nvm.sh"
Then start a new terminal session and see if that does the trick.
Another thing that could be worth double-checking is to see if you have any installed node version aliased as the default one. I'm not sure what nvm does if you don't have a default version specified (if that's even possible). Run nvm ls and see you you have default pointing to any installed version of Node. If not, then set that up by running nvm alias default [version-number].

How to change Node.js version with nvm

I'm using Yeoman to create a project. When I try to use Gulp.js I run the command gulp serve. An error tells me that I need an older version of Node.js (8.9.4), knowing that I've installed the latest version (10.14.1).
So I installed nvm to change the Node.js version. I had to set it into path C:\, and then I run with success: nvm install 8.9.4. And when I try to use it, nvm use 8.9.4, it’s always the latest version that is used:
If I try to use 8.10.0 and then run node -v, it tells me access refused, and the same to any Node.js command.
nvm install 8.10.0 is for installing proposed node version locally.
In order to use it:
nvm use 8.10.0
Note that you need to run this command as administrator.
You can always set default Node.js version:
nvm alias default 8.10.0
Install
nvm install 8.10.0
Use once per terminal
nvm use 8.10.0
Set up as the default for all terminals
nvm alias default 8.10.0
Set up Node.js version for your IDE
For more information check nvm documentation
Switch to a specific Node.js version
nvm use 8.10.0
Switch to the latest Node.js version
nvm use node
Switch to the latest LTS version
nvm use --lts
You can check which versions you have installed by running:
nvm ls
The entry in green, with an arrow on the left, is the current version in use.
Specify a Node.js version on a per-project basis
Version managers, such as RBEnv, allow you to specify a Ruby version on a per-project basis (by writing that version to a .ruby-version file in your current directory). This is kind of possible with nvm in that, if you create a .nvmrc file inside a project and specify a version number, you can cd into the project directory and type nvm use. nvm will then read the contents of the .nvmrc file and use whatever version of Node.js you specify.
If it’s important to you that this happens automatically, there are a couple of snippets on the project’s home page for you to add to your .bashrc or .zshrc files to make this happen.
Here’s the Z shell (executable zsh) snippet. Place this below your nvm configuration:
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
When you change into a directory with a .nvmrc file, your shell will automatically change the Node.js version.
Make sure you run your terminal as an system administrator
nvm use <version> # This should work fine
Without the privilege, I was getting this error:
nvm use 16.14.0
Output:
exit status 5: Access is denied.
You need to edit your .bashrc file.
Add the below to that file. Change the version to your preferred version. This example uses v16.13.1.
It is possible you have something like this already in that file which causes the change back to your previous versions.
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
export PATH="/home/zentech/.local/bin:/home/zentech/.nvm/versions/node/v14.18.2/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin"

How to configure user environment to use the latest node runtime

We initialize node.js using nvm use 0.10.0
How to configure user environment to use the latest node runtime, so that the bash shell gets initialized when I launch a new terminal in Ubuntu
posting this answer for future reference and help.
1st run the following command in the bash.
nvm alias default <the default version you want to set>
then put this in your .bashrc
export NVM_DIR="path/to/.nvm/folder" #<-edit with real value
if [ -s "$NVM_DIR/nvm.sh" ]; then
source "$NVM_DIR/nvm.sh"
fi
NODE_DEFAULT_VERSION=$(<"$NVM_DIR/alias/default")
export PATH="$NVM_DIR/versions/node/$NODE_DEFAULT_VERSION/bin":$PATH
and, source .bashrc
the advantage of this setup is that, whatever node you will install and set as default, it will be available for all bash sessions.
You could put this in your ~/.bashrc:
nvm use 0.10.0

Resources