NVM cannot load default node with default alias set - node.js

My problem is almost the same as this one but i already set the default alias.
Once i open a new console nvm does not load the default node, this problem is also present with some SublimeText3 plugins (eslint, jscs for example).
The only way to make it work is to add nvm use default after loading nvm but i thik i am doing something wrong.
How can i make it work without the hack?
My configuration
brew 0.9.5
nvm 0.26.0 (installed using brew)
zsh (with oh-my-zsh)
I have nvm sourced in .zshenv with th following config:
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
# nvm use default # <-- remove comment to make node available
nvm is configured as follows (result of the command nvm ls):
iojs-v3.0.0
v0.12.7
default -> iojs (-> iojs-v3.0.0)
node -> stable (-> v0.12.7) (default)
stable -> 0.12 (-> v0.12.7) (default)
iojs -> iojs-v3.0 (-> iojs-v3.0.0) (default)

oh-my-zsh includes a nvm plugin, I would recommend you to use it, but first
remove from your .zshenv all changes
remove the nvm install with brew uninstall
install nvm using curl (official installation): curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.26.0/install.sh | bash
this wont configure your profile to use it yet.
edit the file at your users home called .zshrc finding the plugins section and there include nvm save it and source your profile or close and open again your terminal.
use nvm to install a node version and set the default alias

In oh-my-zsh, you can go to [ /etc ] then use [ nano ~/.zshrc ],
find export PATH=$HOME/.nvm/versions/node/v16.5.0/bin/:$PATH change node version what you want

Related

Set node version for a single command using nvm

I have a command that must be run with Node 16 installed, no other version. However, I need to have the latest version of Node installed for regular use.
How can I configure things, perhaps with environment variables, so that just that one command uses Node 16?
Something like nvm use 16 && node -v && nvm use 19 is too slow, but aliasing in .zshrc is an option.
What I've done in one of my Projects is this:
I've switched to node 16: nvm use 16.
After that which node showed this path: /root/.nvm/versions/node/v16.19.0/bin/node
So I've simply created a symlink for this executable: ln -s $(which node) /usr/bin/node16
Finally I switched back the version: nvm use system
Now you can use your default node Version with node and the desired node-version for this command with node16.

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"

Where is node while using nvm?

I am on a MacOS, and I switched from Homebrew Node to NVM, and removed Node from Homebrew but then a lot of my previous packages cannot find Node anymore (Sublime, Heroku etc)...so I have to manually update the location of Node to these packages.
Where is Node while using NVM?
You can get the path to the executable to where node was installed with
nvm which node
Or any of the other NVM special aliases for node versions such as
nvm which default
NVM should set PATH variable to your node installation directory, it does this automatically after:
nvm current use `nvm current`
You can check variables like this:
env | grep NVM
Here is my output:
NVM_DIR=/Users/name/.nvm
NVM_CD_FLAGS=-q
NVM_NODEJS_ORG_MIRROR=https://nodejs.org/dist
NVM_IOJS_ORG_MIRROR=https://iojs.org/dist
NVM_PATH=/Users/name/.nvm/versions/node/v5.9.0/lib/node
NVM_BIN=/Users/name/.nvm/versions/node/v5.9.0/bin
You can locate your node by running which node or command -v node
nvm root command gives us path of the node exe
Trying all the answers in 2023, nothing works:
nvm which node
nvm: Unknown command or option: "which" (see nvm -h for usage)
env|grep NVM
nvm root
nvm: Unknown command or option: "root" (see nvm -h for usage)
And finally!
env|grep nvm
nvm_current_version=v19.6.0
PATH=~/.local/share/nvm/v19.6.0/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
So I guess currently NVM keeps node in ~/.local/share/nvm

Why isn't Node Version Manager (NVM) recognized on Windows?

I am trying to downgrade my version of node
I ran:
npm install nvm
and I exported the bin folder to my Windows path variable,
C:\Program Files (x86)\nodejs\node_modules\npm\bin
but I still get:
'nvm' is not recognized as a an internal or external command.
Should I be adding another path to my path variable?
nvm was designed for Linux. nvmw, which is completely different, broke around node v0.10.30. Try NVM for Windows.
NVM can be used to manage various node version :
Step1: Download
NVM for Windows
Step2: Choose nvm-setup.zip
Step3: Unzip & click on installer.
Step4: Check if nvm properly installed, In new command prompt type nvm
Step5: Install node js using nvm :
nvm install <version> : The version can be a node.js version or "latest" for the latest stable version
Step6: check node version - node -v
Step7(Optional)If you want to install another version of node js - Use STEP 5 with different version.
Step8: Check list node js version - nvm list
Step9: If you want to use specific node version do - nvm use <version>
NVM Installation & usage on Windows
Below are the steps for NVM Installation on Windows:
NVM stands for node version manager, which will help to switch between node versions while also allowing to work with multiple npm versions.
Install nvm setup.
Use command nvm list to check list of installed node versions.
Example: Type nvm use 6.9.3 to switch versions.
For more info
As an node manager alternative you can use Volta from LinkedIn.
I created a universal nvm that works on both Unix (bash) and Windows, base on another simple nvm.
It doesn't need admin on Windows, but requires PowerShell 4+ and the right to execute scripts.
https://www.npmjs.com/package/#jchip/nvm#installation
The first thing that we need to do is install NVM.
Uninstall existing version of node since we won’t be using it anymore
Delete any existing nodejs installation directories. e.g. “C:\Program Files\nodejs”) that might remain. NVM’s generated symlink will not overwrite an existing (even empty) installation directory.
Delete the npm install directory at C:\Users[Your User]\AppData\Roaming\npm
We are now ready to install nvm. Download the installer from https://github.com/coreybutler/nvm/releases
To upgrade, run the new installer. It will safely overwrite the files it needs to update without touching your node.js installations. Make sure you use the same installation and symlink folder. If you originally installed to the default locations, you just need to click “next” on each window until it finishes.
Credits
Directly copied from : https://digitaldrummerj.me/windows-running-multiple-versions-of-node/
I will list two ways. You can choose one Whichever works for you.
1. Using installer
Download nvm-setup.zip and unzip the file and install it, keeping the configurations default.
1. Use curl
Copy the below command and run it in your terminal
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
After this reopen/open terminal and check the nvm version runing below command.
nvm -v
And that's it.
If someone is looking for install on Window 11! Not directly relevant here, but might be useful.
It is immaterial if you install NVM (version 1.1.9.) say after the node (16.15.1) is already installed. During the nvm installation process, it asks for the right to manage the existing node version and symlinks that.
Get the version from the GitHub repo, I opted for the zip version.
https://github.com/coreybutler/nvm-windows/releases
Double click the application and it is just a few steps.
1.downlad nvm
2.install chocolatey
3.change C:\Program Files\node to C:\Program Files\nodejsx
emphasized textThe first thing that we need to do is install NVM.
website :
https://learn.microsoft.com/en-us/windows/nodejs/setup-on-windows
So this answer is for windows users that are using git bash or some other console emulator like cmder ... if you're using CMD this solution will not work for you also why? why are you still using CMD?
I know this is a pretty old post but I just achieved this yesterday and wanted to add my answer for anyone looking to do the same.
First check if you have .bashrc profile in your home directory by typing ls -alh ~ (by default this doesn't exist)
if it doesn't exist type this command to generate a .bashrc profile with default values in it cat /etc/bash.bashrc > ~/.bashrc (if it does exist skip this step)
Download and run the nvm install script as provided in the nvm docs page curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash (make sure you do this in your home directory)
then edit the new generated .bashrc profile file you created above; use nano/vim to do that nano ~/.bashrc and add the following to the bottom of the file export NVM_DIR="$HOME/.nvm" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm and save your .bashrc file with the changes.
lastly source your .bashrc file by typing source ~/.bashrc
verify installation nvm --version
and now you have nvm installed and you can use the commands as per https://github.com/nvm-sh/nvm#usage
First off, I use nvm on linux machine.
When looking at the documentation for nvm at https://www.npmjs.org/package/nvm, it recommendations that you install nvm globally using the -g switch.
npm install -g nvm
Also there is a . in the path variable that they recommend.
export PATH=./node_modules/.bin:$PATH
so maybe your path should be
C:\Program Files (x86)\nodejs\node_modules\npm\\.bin
An alternative to nvm-windows, which is mentioned in other answers would be Nodist.
I've had some issues with nvm-windows and admin privileges, which Nodist doesn't seem to have.
I know I'm late here but this may help in the future if someone looking for NVM to install in Windows or linux
run this command in cmd
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

Managing multiple Node.js versions with nvm in a tmux session

Short question
How can I get nvm to work with tmux?
Path Problems
As MarkHu surmised in his answer, the problem is related to the PATH. Below is a comparison of the path in bash before entering tmux and after starting tmux. (I replaced each : in the path with a line break to make it easier to compare.)
Now I'm curious as to:
Why are /usr/local/sbin and /Users/matthew/bin are duplicated in the path?
Why did /Users/matthew/.nvm/v0.11.5/bin along with /usr/local/sbin and /Users/matthew/bin get moved to the end of the path?
Background
I'm running OS X 10.8.4 Mountain Lion. I installed tmux v1.8 and Node.js using:
brew install tmux
brew install node
I then decided I wanted to manage multiple node version, so I installed nvm using:
curl https://raw.github.com/creationix/nvm/master/install.sh | sh
If I'm not in a tmux session, nvm appears to work correctly. When I start a tmux session though, it finds the Node.js installed by Homebrew instead of using the nvm version. Any thoughts on how to get nvm to work with tmux?
$ which node
/Users/matthew/.nvm/v0.11.5/bin/node
$ tmux
$ which node
/usr/local/bin/node
$ nvm use v0.11.5
Now using node v0.11.5
$ which node
/usr/local/bin/node
I just run:
nvm deactivate
nvm use x.x
That seems to work. You can script out the needed version depending on the project you're working on.
Check your $PATH environment var before and after.
There is a difference between setting it in ~/.bash_profile and ~/.bashrc depending on how you have those configured.
Also, you may want to read https://unix.stackexchange.com/questions/15453/using-environment-variables-in-tmux-conf-files if you think other node.js-related vars may need to be set.

Resources