Set node version for a single command using nvm - node.js

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.

Related

Using nvm in a makefile to switch node environment

I am trying to use Makefile to build Node projects. As a first step, I want to set node version using NVM. I tried the following code for that:
.PHONY: viz-ui
viz-ui:
. ${HOME}/.nvm/nvm.sh && nvm use 14.17.0
node -v
I set nvm use 16 before running make to test the output. After running make viz-ui, I get the following output:
. /home/vijayth2/.nvm/nvm.sh && nvm use 14.17.0
Now using node v14.17.0 (npm v6.14.13)
node -v
v16.14.2
In short, the node switch to 14.17.0 is not reflecting in the makefile, how can i fix it? Or what is the gap in my understanding of this?
Every line of a recipe is run in a brand-new shell. Therefore, when you reach node -v line, it is executed in a new shell that knows nothing about previous one that has set up different node version (this was executed in a separate shell and long gone).
You have basically two options:
Rewrite the recipe so that it all runs in a single shell invocation, e.g.:
.PHONY: viz-ui
viz-ui:
. ${HOME}/.nvm/nvm.sh && nvm use 14.17.0; \
node -v
or
Make use of .ONESHELL directive, so that all the recipe is executed in a single shell.

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

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

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