Problem choosing node version in Terminal - MacOS Ventura - node.js

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!

Related

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

npm install zsh: command not found: npm

I just reinstalled node on my laptop and trying to install packaged in project. I installed node via brew and Im using MacOS.
npm install
zsh: command not found: npm
The solution below actually solved my issue
https://superuser.com/questions/1403007/zsh-npm-node-nvm-command-not-found-after-installing-ohmyzsh
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
this line of code on your .zshrc file to do that type vim ~/.zshrc than add the following lines in the bottom and to save the file click ctrl than press :wq! to save file in vim you can also use nano
Your binaries for npm are in /usr/local/share/npm/bin. Is this in your path environment variable?
If not export it at ~/.zshrc with export PATH=/usr/local/share/npm/bin:$PATH.
For me, I encountered the same issue as you did.
How I fixed:
First, checking whether npm is installed or not by checking its version:
npm --version
If the terminal still shows no npm not found, then type:
nvm install --lts
This command will trigger updating node package manager to the latest version.
Hope it helps.
You should run nvm use node every time you startup a new terminal.

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.

why restarting reverts the version of node

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

Resources