How to configure user environment to use the latest node runtime - node.js

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

Related

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

Use aliases with exec in a non-interactive shell

During a deployment process I run yarn install over an SSH connection in a non-interactive shell. Since an alternative node.js version (default version 8.x on the server is too low) must be used, an alias is created and loaded in the bash script over the SSH connection:
shopt -s expand_aliases
source ~/.bashrc
node -v
The .bashrc script looks like:
export PATH=/opt/plesk/node/12/bin/node:$PATH
alias node="/opt/plesk/node/12/bin/node"
Running node -v will print the correct version 12.x. The correct version is also output when executing JavaScript code:
node -e "console.log(process.versions.node);"
But yarn calls node internally with exec (https://github.com/yarnpkg/yarn/blob/master/bin/yarn#L20). And this will call the default version of node instead of the alias version. That means yarn versions lists node 8.x.
This is only the case with a non-interactive bash. If I call this in an interactive shell, yarn internally uses the correct node version 12.x of the alias.
How can I run yarn in a non-interactive bash with the correct node version?

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"

Restart Shell in Vagrant

I'm provisioning a VirtualBox server running Ubuntu Trusty using Vagrant and installing nvm. After installation I am given the following instructions:
Close and reopen your terminal to start using nvm
When I install manually this is simple (I log out and back in) but how can I automate this in the vagrantfile?
It's probably a shorthand for "restart your shell". Starting a new login shell with something like exec bash --login (with suitable adaptations for other shells) should accomplish the same thing, but isn't easily scriptable (that is, the currently executing script will terminate).
In reality, I would guess sourcing the updated dot files should suffice. I.e. if $HOME/.bashrc was updated,
. $HOME/.bashrc
would bring in the changes to the current script in the vast majority of cases. (If something was removed, sourcing will obviously not remove that setting from the current environment in the typical case.)
Of course, if the process which would print this prompt has already terminated, new scripts you run should already receive the updated environment.
I was actually looking for the exact same thing. Facing the same issue with nvm.
Unfortunately, I was unable to find anything so far with regards to 'restarting' the terminal, but I followed the suggestion from the nvm install script and it seems to have worked. I was able to install node.
My Vagrantfile looks like this:
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/focal64"
config.vm.box_version = "20220517.0.0"
config.vm.provision :shell, path: "setup.sh", privileged: false
end
Where the setup.sh contains:
#!/usr/bin/env bash
# Get nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
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
# Install Node 16
nvm install 16
Result
As mentioned, have not yet found a way to 'restart' the terminal - but would also like to know how that's done.

Resources