Node version manager issue on mac - node.js

I am installing nvm on mac using the command
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash
After installed I am opening new tab. But I keep getting error zsh: command not found: nvm. Thank you in advance.

For me the issue was I was using bash before and now have zsh which was only inserting the env loading lines in .bashrc. I had to copy the following lines manually into .zshrc.
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
Check and add these lines if they are not in it already.
This has been mentioned in the wiki as well.

My issue was exactly what deepakchethan has mentioned. I just removed the bash_profile file and reinstalled. Nvm with same command and it worked properly this time.

Related

Node doesn't start without source ~/.bash_profile command

I have to use source ~/.bash_profile for node command to work on my terminal. All the node commands work just fine after using source ~/.bash_profile. I used nvm to install Node LTS(v14.18.0).
My .bash_profile
export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh
Is there any way to fix my node installation?
MacOS uses zsh as the default shell and not bash. You will need to create a ~/.zshrc file and then run the install script of nvm again.
You can probably skip the rerunning the install script by manually inserting in .zshrc file the following:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
"Since macOS 10.15, the default shell is zsh and nvm will look for .zshrc to update, none is installed by default. Create one with touch ~/.zshrc and run the install script again." - from nvm docs on github.
https://github.com/nvm-sh/nvm#troubleshooting-on-macos
I was facing the same issue and the above solution didn't work for me.
Finally the solution that did work for me was adding these lines to ~/.zshrc file.
source ~/.nvm/nvm.sh

How to write a .nvmrc file which automatically change node version

Hi I have two projects one in angularjs 4.4.7 and another in angular 6 version. I need to switch between node version for this. I tried using NVM which is working manually. How to handle the version change inside the angularjs program to change the node version when automatically the latest angular page gets loaded. Is there a possible way like that. I went through the #avn also but how to create the .node-version file. Can someone help with any link or correct sample steps
As #Aditya-M-P has already mentioned you can run the following command inside your projects root directory to generate the .nvmrc to set a desired NodeJS version for you project to work properly:
node -v > .nvmrc
It will generate something like this inside your .nvmrc file:
v10.16.2
Also using 10.16.2 without the v letter will work just fine.
However, in the official documentation in the .nvmrc section it never mentions that once you get this file created, the specified node version will be loaded automatically.
So that's not enough, you need to run the command below so that nvm can look for the .nvmrc file to load the specified version:
nvm use
Here it is a gif for demoing purpose:
To autoload the specified node version:
You need to add something else to your shell configuration depending on what you use bash or zsh
To get the exact configuration for each of them, please follow the instructions in the corresponding shell config section.
In my case I'm using zsh so I do need to add this at the end of my .zshrc file and here is the image that confirms it works like a charm:
# place this after nvm initialization!
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
I hope it could be useful for anyone else facing the same question! 😎
Check the README from nvm's repo on GitHub. Solutions have been given there.
Shell Integraton
For bash, put the following at the end of your $HOME/.bashrc, the shell will change the node version according to the .nvmrc file under the dir.
find-up () {
path=$(pwd)
while [[ "$path" != "" && ! -e "$path/$1" ]]; do
path=${path%/*}
done
echo "$path"
}
cdnvm(){
cd "$#";
nvm_path=$(find-up .nvmrc | tr -d '[:space:]')
# If there are no .nvmrc file, use the default nvm version
if [[ ! $nvm_path = *[^[:space:]]* ]]; then
declare default_version;
default_version=$(nvm version default);
# If there is no default version, set it to `node`
# This will use the latest version on your machine
if [[ $default_version == "N/A" ]]; then
nvm alias default node;
default_version=$(nvm version default);
fi
# If the current version is not the default version, set it to use the default version
if [[ $(nvm current) != "$default_version" ]]; then
nvm use default;
fi
elif [[ -s $nvm_path/.nvmrc && -r $nvm_path/.nvmrc ]]; then
declare nvm_version
nvm_version=$(<"$nvm_path"/.nvmrc)
declare locally_resolved_nvm_version
# `nvm ls` will check all locally-available versions
# If there are multiple matching versions, take the latest one
# Remove the `->` and `*` characters and spaces
# `locally_resolved_nvm_version` will be `N/A` if no local versions are found
locally_resolved_nvm_version=$(nvm ls --no-colors "$nvm_version" | tail -1 | tr -d '\->*' | tr -d '[:space:]')
# If it is not already installed, install it
# `nvm install` will implicitly use the newly-installed version
if [[ "$locally_resolved_nvm_version" == "N/A" ]]; then
nvm install "$nvm_version";
elif [[ $(nvm current) != "$locally_resolved_nvm_version" ]]; then
nvm use "$nvm_version";
fi
fi
}
alias cd='cdnvm'
Cause there's no hook support in Bash, the solution above is ugly.
For zsh, put this into your $HOME/.zshrc
# place this after nvm initialization!
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
A Better Solution
A better solution is to use nodenv. I'm not kidding, nodenv is very different from nvm, and n.
nodenv is a member of the rbenv family. These version managers have big advantages over the others.
It changes node version without modifying environment variable PATH time by time, because it uses shim executables. This makes it having a builtin support to switch node version automatically.
Auto version switch in nodenv doesn't have to be hooked on chpwd to do periodical check for directory change. The version selection is delayed to when node command is executed.
The commands in nodenv are implemented in scripts. While, commands from nvm are implemented in functions, which means all the 4000+ line of code have to be parsed on shell startup and increases the shell init time dramatically. nodenv initializes much faster.
References
nodenv/nodenv
As pointed out in the GitHub issue thread related to this on the nvm repository, you may run the following command in each of your Angular project folders:
$ node -v > .nvmrc
Note that you need to first switch to the right version of node in each of your projects, before running the command above.
What's happening in the command:
node -v will out the current version of node to stdout.
The > symbol will then redirecting the output to a file called .nvmrc (it will overwrite if something already exists with the same file name).
Read more bash redirections under the REDIRECTION section under the bash man page: https://linux.die.net/man/1/bash
When you cd into your target directories, nvm will now first read the file, and auto-switch to the correct version.
After you create the .nvmrc file at the root of your project with the node version you need in that project, something like
v12.20.0
You should be able to cd into the project folder and run nvm use. This will print something like this:
Found '/Users/you/myproject/.nvmrc' with version <v12.20.0>
Now using node v12.20.0 (npm v6.14.8)
There is no automated way AFAIK, provided out of the box by NVM except by creating a bash script that does this for you which the NVM documentation covers in detail here

How do I access my nvm node version number from .bash_profile to customize my terminal prompt?

I want to customize my terminal prompt so that it displays whatever version of node that I'm using through nvm. So, for example, the terminal prompt my read:
[current directory] : [node version] $
Does anyone know how to modify .bash_profile to display the current nvm node version?
Add this to your ~/.bashrc :
PS1='\w : $(node --version) \$'
or
PS1='\w : $(nvm run node --version) \$'
In bash:
nvm=$([[ $NVM_BIN =~ ([^/]+)/bin$ ]] && echo "${BASH_REMATCH[2]}" || echo "system")
PS1="($nvm) $PS1"
This is much faster than running nvm directly. For timings and zsh, see here.

Bash complains "have not a command" when login

I have a file named "upstart" in /etc/bash_completion.d/ with the following content:
# bash-completion for the upstart event-based init replacement
# (http://upstart.ubuntu.com / https://launchpad.net/upstart)
#
# We don't provide completion for 'init' itself for obvious reasons.
have initctl &&
_upstart_jobs()
{
initctl list|awk '{print $1}'|sort -u
} &&
The confusing part is the line have initctl &&, I have configured bash to source
all files in /etc/bash_completion.d/ and every time I login it complains
that command have cannot be found. What is that line for?
Short Answer: maybe you should install bash-completion package.
In general, you don't need to manually source the files in /etc/bash_completion.d/.
It is automatically imported by bash_completion script.
bash_completion script is called by /etc/bash.bashrc (at least for Ubuntu and Arch Linux), which would be called by default.
In Ubuntu, bash_completion script is in bash-completion package and placed in /etc/bash_completion
In Arch Linux, bash_completion script is in bash-completion package and placed in /usr/share/bash-completion/bash_completion
Therefore, installing bash-completion script would help if you are using Ubuntu or Arch Linux.

how to make nvm automatically sourced upon login

I am using nvm to manage the node version.
I like to make nvm sourced upon login, so I dont have to do it manually everytime I login.
in my user home directory, there's a .bashrc file. I appended following two lines to the end of the file. then restart my mac os. after I login, nvm is not sourced. I have to manually run them again. coudln't figure out whats wrong. please help.
. ~/nvm/nvm.sh
nvm use 0.8.20
You need to put into your .bash_profile this line
[[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh
Source the file (source ~/.bash_profile) or reopen the shell and then in the shell execute:
$ nvm alias default <version>
That would load node in any new shell you open.
Using .nvmrc file
It turns out that creating a .nvmrc file in your home directory is enough. It is loaded automatically when you open the terminal.
To create the .nvmrc file with the version we want to run (0.12.2) do this:
echo '0.12.2' > ~/.nvmrc
If that for some reason doesn't work, force load the nvm from your .bashrc
But only if you're running in interactive mode.
Add the command to run nvm use 0.12.2 (or any other version you'd like to run) if the shell is launched in interactive mode to the end of .bashrc.
echo '[[ $- == *i* ]] && nvm use 0.12.2' >> ~/.bashrc
Boom, done! :)
For Mac at least, open your .profile file, and add this line in
[[ -s $HOME/.nvm/nvm.sh ]] && source "$HOME/.nvm/nvm.sh"
# Load NVM into a shell session *as a function*
Done, bingo bango, GG.
Please use the official website carefully as I used
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | zsh from https://github.com/nvm-sh/nvm and it automatically does my work from installing to load automatically setup.

Resources