Why doesn't "n" downgrade my node version on a Mac? - node.js

I’m using Mac Big Sur. I want to downgrade the version of Node. I tried the below (installing without sudo gives me permission errors) …
$ sudo n 14.15.1
installed : v14.15.1 to /usr/local/bin/node
active : v14.17.6 at /usr/local/opt/node#14/bin/node
But I still get the current version when I check
$ node -v
v14.17.6
Fwiw, here’s what I see with “which node”
$ which node
/usr/local/opt/node#14/bin/node
Edit: My $PATH when I run 'echo $PATH' ...
/usr/local/opt/node#14/bin:/Users/davea/.rvm/gems/ruby-2.7.1/bin:/Users/davea/.rvm/gems/ruby-2.7.1#global/bin:/Users/davea/.rvm/rubies/ruby-2.7.1/bin:/usr/local/opt/node#14/bin:/Users/davea/.nvm/versions/node/v14.18.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.rvm/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.rvm/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.rvm/bin
Edit 2: PATH vars for both regular user and sudo user ...
$ env | grep PATH
PATH=/Users/davea/.rvm/gems/ruby-2.7.1/bin:/Users/davea/.rvm/gems/ruby-2.7.1#global/bin:/Users/davea/.rvm/rubies/ruby-2.7.1/bin:/Users/davea/.nvm/versions/node/v14.18.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.rvm/bin
$ sudo env | grep PATH
PATH=/Users/davea/.rvm/gems/ruby-2.7.1/bin:/Users/davea/.rvm/gems/ruby-2.7.1#global/bin:/Users/davea/.rvm/rubies/ruby-2.7.1/bin:/Users/davea/.nvm/versions/node/v14.18.1/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.cabal/bin:/Users/davea/.ghcup/bin:/Users/davea/.rvm/bin

The n message is telling you that the version it just "installed" and the version that is "active" are different. You have two versions of node installed, and the active version is the one that is first in the PATH.
The active version is /usr/local/opt/node#14/bin/node. I don't recognise that path, not sure what was used to install that. It must be in your PATH variable and there might be a clue in your login script as to what added /usr/local/opt/node#14/bin to the PATH?
To get the n installed version of node to be the active version, you could delete the other copy of node, or put /usr/local/bin earlier in PATH so it is found first, or simplest remove /usr/local/opt/node#14/bin from your PATH variable.

If you're going to use multiple node versions like at nvm:
nvm install <version>
Change version with:
nvm use <version>
Docs:
https://github.com/nvm-sh/nvm

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.

n doesn't change node.js version

I have 12.19.0 and 14.15.4 on ubuntu. I can't use the n command to change active node version. Either the below does not work:
# which node
/root/node-12.19.0/bin/node
# n stable
installed : v14.15.4 to /usr/local/bin/node
active : v12.19.0 at /root/node-12.19.0/bin/node
# n 14.15.4
installed : v14.15.4 to /usr/local/bin/node
active : v12.19.0 at /root/node-12.19.0/bin/node
# echo $N_PREFIX/bin
/bin
How can I change the active version of node.js with n? Thanks.
Looks like you have two versions installed and also you don't have N_PREFIX set (since that showed just /bin, and N_PREFIX would point to one directory above the /bin level). To fix it, I would take a look at your PATH wherever you have that set (probably something like your .bashrc or .profile), figure out how you ended up with an install at /root/node-12.19.0 and get rid of that one, and then you should be good. The default for N_PREFIX is /usr/local so that's fine.

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

NPM not found when using NVM

I have installed node/npm using the nvm documentation.
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.29.0/install.sh | bash
Then:
nvm install node
At this point node is working but the npm command result with:
npm: command not found
How can I have npm to work correctly ?
I found out that this was a conflict with a previous versions of npm that have not been removed properly despite a apt-get remove node.
I solved it by reinstalling npm from scratch:
rm -R ~/.npm ~/.nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash
nvm install node
I found the solution here.
If you run NVM-Windows, don't forget to run nvm on. (this solve the problem as title for me.)
I fixed this by doing this command:
$ command -v npm
and then reopen the shell window.
I solved it by uninstalling all problematic node versions (e.g. v14 below) and reinstalling it.
The problem:
node --version; npm --version;
v14.17.1
Command 'npm' not found, but can be installed with:
sudo apt install npm
The solution:
nvm deactivate
echo "All versions BEFORE:"
nvm_ls
# uninstall all 14.* versions
for v in $(nvm_ls 14); do nvm uninstall $v; done
echo "All versions AFTER:"
nvm_ls
# reinstall version 14
nvm install 14
# and now it has npm too
node --version; npm --version
which node; which npm
# v14.17.1
# 6.14.13
# /home/user/.nvm/versions/node/v14.17.1/bin/node
# /home/user/.nvm/versions/node/v14.17.1/bin/npm
If you use Windows OS, make sure you removed the existing nodejs and npm.
In my case, it worked well after I remove the C:/Program Files/nodejs.
Reference is here.
During nvm installation, make sure the selected path must NOT exist.
This problem especially happens in windows which happens because of missing admin rights for cmd.
If you are using Git bash
Go in installation directory e.g C:\Program Files\Git
Right click properties -> compatibility.
Tick the checkbox with label -> Run as administrator.
Run the git bash again & execute npm list and then npm use 'version_to_be_used'
Same goes for Cmd
One possible reason is the NVM symlink is invalid.
But first, check if both NVM_HOME & NVM_SYMLINK is already set in environment path.
If not, maybe some problem with your nvm installation and u might want to reinstall.
Using explorer, open the symlink folder to check if the folder is valid.Default Symlink path in Windows: C:\Program Files\nodejs. Symlink appears as a normal shortcut in Windows explorer.
If you see node files in there, then you're fine.
If the folder is invalid, delete the symlink.
Then, execute nvm ls and nvm use <desired node version>, this step will re-create the correct symlink.
Restart CMD and test nvm current, node -v, npm -v
For Windows:
nvm creating symlink from installed node path like c:\program files\node to the c:\users<your user>\AppData\nvm<node ver>
So check:
Your basic node path in the PATH variable.
Your npm is inside c:\users<your user>\AppData\nvm<node ver>\nmp and this path is also int the PATH variable.
You could also run
source ~/.bashrc
and try to run again on the same bash terminal where you downloaded the install.sh the command:
npm -v
I went through a similar issue recently and solved it by setting the npm mirror to npm_mirror https://github.com/npm/cli/archive/refs/tags/
The default npm mirror (https://github.com/npm/cli/archive) was a broken link.
so run
nvm npm_mirror https://github.com/npm/cli/archive/refs/tags/
I had the same issue while any new terminal instance started up the message 'npm not found' was shown. I noticed that I had defined (probably) custom paths to npm and node in ~/.bashrc. Deleting them (keeping the paths for nvm) resolved the problem.
This helped me: https://github.com/coreybutler/nvm-windows/issues/548#issuecomment-768297716 Adding quotes to NVM_SYMLINK environment variable: "C:\Program Files\nodejs" instead of C:\Program Files\nodejs.
Install node using node source distribution:
curl -sL https://deb.nodesource.com/setup_[version].x | bash -
apt-get install -y nodejs
[version] = the wanted version. See the repository to choose the correct: NodeSource Node.js Binary Distributions

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

Resources