How do I find the currently activated version with nvm? - nvm

I use nvm and want to find out which version of node is currently running.
If the system version is being used, I want to see the string system.
Ideally, I'm looking for a string to put in my prompt.

run this in terminal
nvm current
or
node -v
nvm ls
for list all version
nvm use version_name
for use that version

In bash:
[[ $NVM_BIN =~ ([^/]+)/bin$ ]] && echo "${BASH_REMATCH[2]}" || echo "system"
For zsh, first do:
setopt BASH_REMATCH
This is much faster than using nvm current, especially for use in a prompt:
$ time nvm current
system
real 0m0.188s
user 0m0.149s
sys 0m0.042s
Compared with:
$ time [[ $NVM_BIN =~ ([^/]+)/bin$ ]] && echo "${BASH_REMATCH[2]}" || echo "system"
real 0m0.009s
user 0m0.002s
sys 0m0.007s
system
Almost 0.2 seconds vs only 0.009 seconds.

Related

How to use npm version to increment the counter behind the prepatch / preid suffix

I'm trying to find out how to correctly use npm version with prepatch (also premajor or preminor) / preid options to increment the counter behind the suffix.
e.g.:
I have a v.0.5.22 and want to append -rc
I used the command npm version prepatch --preid rc
then I get v.0.5.23-rc.0, fine so far.
but the next time I'm using the same command I end up with v.0.5.24-rc.0,
what I want is to get v.0.5.23-rc.1 instead
How can I only increment the counter behind -rc. and keep the patch number?
Or am I misunderstanding the purpose of prepatch / preid?
Alright, I'll answer my own question for future reference.
After having v.0.5.23-rc.0 the command to only bump the number behind the . has to be:
npm version prerelease
Then I would get v.0.5.23-rc.1.
An alternative to npm version would be to write a bash script.
Ended up with this, fetching the version from the package.json
#!/usr/bin/env bash
set -e
packageJsonLocation="../package.json"
current_version=$(grep '"version":' $packageJsonLocation | cut -d\" -f4)
pre=0
if [ -z "${current_version}" ]; then
echo "No version found in package.json"
exit 1
fi
if [[ $current_version =~ ^.*-.* ]]; then
# increment prerelease version
IFS='-' read -ra array_pre <<< "$current_version"
IFS='.' read -ra array <<< "${array_pre[0]}"
pre=$(( ${array_pre[1]} + 1))
else
IFS='.' read -ra array <<< "${current_version}"
fi
new_version="${array[0]}.${array[1]}.${array[2]}-${pre}"
echo "Pre version: ${current_version} -> ${new_version}"
# Output
2.9.3 -> 2.9.3-0
and
2.9.3-1 -> 2.9.3-2

Node reverts to old version and NVM disappears when I close terminal and restart

Issue
I have installed NVM. Each time I close the terminal and open it back up, NVM disappears and my node version rolls back from 14 to 12.
Every time I open my terminal back up, I notice the node version rolls back. I try to use nvm, and will get bash: nvm: command not found. I have to run this script again to get it working every time.
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
I noticed the meta description when you google NVM says it's designed to be used per-user and invoked per shell. However, the setup article linked in the first SO post below mentioned installing, killing the terminal, and opening it back up with nvm still being loaded.
Anyone know what I can do so that I don't have to reinstall nvm every time I open the terminal?
Steps taken to resolve
I have reviewed this SO post and also this one. From those two I have tried the following suggestions:
I have run nvm alias default v14.17.1 in my root directory
I have pasted the following script into both .zshrc and .bashrc
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
Edit: I am on MacOS Big Sur 11.5.2

How to get new terminal windows to have node version 12 instead of 10?

I've installed node version 12 using the below commands.
$ nvm install 12 -g
# note - nvm use 12.13.1 has same effect
v12.13.1 is already installed.
Now using node v12.13.1 (npm v6.12.1)
When I type $ node -v to confirm the node version, the command line reads v12.13.1.
I have also set it as my default node version.
$ nvm alias default 12.13.1
default -> 12.13.1 (-> v12.13.1)
Yet when I open a new window I still see version 10.
$ node -v
v10.17.0
and I have to
$ nvm use 12
every time I open a new window
and then I have
$ node -v
v12.13.1
How can I have 12 be my default without doing use every time ?
This is on Ubuntu, my .bashrc includes:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion" # This loads nvm bash_completion
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
I removed npm by removing the ~/.npm folder and then reinstalled npm with:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash
and version 12 of node with
nvm install 12
node -v
This fixed it and now new terminal windows now show
node -v
v12.13.1

How to run "nvm" in "oh my zsh"?

In the system there is a nodejs, installed through nvm. The command is not running npm.
Console is Oh my zsh
You can use zsh-nvm or enable it yourself by adding following lines to your ~/.zshrc
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
Extra:
For faster shell initialization, I use lazynvm which only loads node when needed
lazynvm() {
unset -f nvm node npm
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm
}
nvm() {
lazynvm
nvm $#
}
node() {
lazynvm
node $#
}
npm() {
lazynvm
npm $#
}
Reference: Lazy load nvm for faster shell start
Switching from Bash to Oh-My-Zsh
If you already have nvm installed and you're switching from bash to oh-my-zsh you can simply open up your .zshrc file and add the nvm plugin that is included with oh-my-zsh:
Open your zsh config file.zshrc in nano with this command: nano ~/.zshrc
Scroll down to where it shows plugins=(git) and add nvm inside the parentheses to make it show as plugins=(git nvm) (separate plugins with spaces)
Press control + O (on macOS), then enter, to save, then press control + X to exit
Then open a new terminal window/tab and enter nvm ls to confirm it works. Note that you must open a new window/tab for your shell to use the newly updated .zshrc config (or enter source ~/.zshrc, etc.)
Source: https://github.com/robbyrussell/oh-my-zsh/tree/master/plugins/nvm
This worked for me on Ubuntu 20.04.
Install or update nvm
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
Add in your ~/.zshrc
echo 'export NVM_DIR=~/.nvm' >> ~/.zshrc
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"' >> ~/.zshrc
Load in the current shell environment
source ~/.zshrc
Check the nvm version
nvm -v
use homebrew to install nvm
brew install nvm
edit your system configuration
vim ~/.zshrc # or vim ~/.bashrc
export NVM_DIR=~/.nvm
esc > :wq
save file
reload the configuration
source $(brew --prefix nvm)/nvm.sh
view nvm version
$ nvm --version
# 0.36.0
enjoy it.
A much easier solution is to use the nvm plugin that is shipped by default:
It also automatically sources nvm, so you don't need to do it manually
in your .zshrc
git clone https://github.com/nvm-sh/nvm.git ~/.nvm
cd ~/.nvm && git checkout v0.35.1 (current latest release)
Add nvm to your ~/.zshrc. Ex: plugins=(... nvm)
I discovered that there is a nvm plug-in shipping with oh-my-zsh (that's different from lukechilds plugin). After short inspection, I think it adds the necessary modifications to .zshrc when loading, so simply adding nvm to the plugins list in .zshrc should work as well (and it does for me).
I did not find any more details on that default nvm plugin via google so I don't know whether this is the "go-to" solution.
Add this code to .zshrc on your user directory
export NVM_DIR="$HOME/.nvm"
[ -s "/usr/local/opt/nvm/nvm.sh" ] && . "/usr/local/opt/nvm/nvm.sh" # This loads nvm
[ -s "/usr/local/opt/nvm/etc/bash_completion.d/nvm" ] && . "/usr/local/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion
Then run this code on your terminal:
source ~/.zshrc
With Linux (Ubuntu 20.04, 22.04 and 22.10)
With your favorite editor, you edit ~/.zshrc
nano or vi ~/.zshrc
At the end of the file, you add :
# NVM
export NVM_DIR=~/.nvm
[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh"
And then you run :
source ~/.zshrc
I strongly suggest using christophemarois' approach to lazy loading nvm (node, npm and global packages) in order to avoid slow shell starting times:
# Add every binary that requires nvm, npm or node to run to an array of node globals
NODE_GLOBALS=(`find ~/.nvm/versions/node -maxdepth 3 -type l -wholename '*/bin/*' | xargs -n1 basename | sort | uniq`)
NODE_GLOBALS+=("node")
NODE_GLOBALS+=("nvm")
# Lazy-loading nvm + npm on node globals call
load_nvm () {
export NVM_DIR=~/.nvm
[ -s "$(brew --prefix nvm)/nvm.sh" ] && . "$(brew --prefix nvm)/nvm.sh"
}
# Making node global trigger the lazy loading
for cmd in "${NODE_GLOBALS[#]}"; do
eval "${cmd}(){ unset -f ${NODE_GLOBALS}; load_nvm; ${cmd} \$# }"
done

Syntax error in rvm bash scripts

My rvm is not working, probably due to an error. When I open new console, it says:
-bash: /Users/amorfis/.rvm/scripts/cd: line 14: syntax error near unexpected token `('
-bash: /Users/amorfis/.rvm/scripts/cd: line 14: ` cd() { __zsh_like_cd cd "$#" ; }'
It's hard to say where the script .rvm/scripts/cd is called. When I remove this line from ~/.bash_profile:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
there is no error. But when I issue source $HOME/.rvm/scripts/rvm... still there is no error.
My system is Mac OS X 10.9.4
rvm --version:
rvm 1.25.29 (stable) by Wayne E. Seguin <wayneeseguin#gmail.com>, Michal Papis <mpapis#gmail.com> [https://rvm.io/]
UPDATE
Other scripts in ~/.rvm/scripts:
alias
aliases
autolibs
base
cd
cleanup
cli
completion
cron
db
disk-usage
docs
env
extras
fetch
fix-permissions
functions
gemsets
group
hash
help
hook
info
initialize
install
irbrc
irbrc.rb
list
maglev
manage
migrate
monitor
mount
notes
osx-ssl-certs
override_gem
patches
pkg
prepare
repair
requirements
rtfm
rubygems
rvm
set
snapshot
tools
upgrade
version
wrapper
zsh
My ~/.bash_profile looks like this:
#...not important stuff
source ~/.bashrc
[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*
And in my ~/.bashrc I have this line (and few others):
[ -s "/Users/amorfis/.scm_breeze/scm_breeze.sh" ] && source "/Users/amorfis/.scm_breeze/scm_breeze.sh"
When I remove this line, the error is also gone. And again, it still doesn't show when I run source ~/.scm_breeze/scm_breeze.sh
Scm breeze is installed from here: https://github.com/ndbroadbent/scm_breeze
In source ~/.scm_breeze/scm_breeze.sh there is such piece of code:
if ! type ruby > /dev/null 2>&1; then
echo "Now in if"
# If Ruby is not installed, fall back to the
# slower bash/zsh implementation of 'git_status_shortcuts'
source "$scmbDir/lib/git/fallback/status_shortcuts_shell.sh"
fi
I expected the "if" statement is the problem. So I did this. Added such code before the if:
echo "Now lets try"
if ! type ruby > /dev/null 2>&1; then
echo "trying"
fi
echo "tried"
and inside if, as the first line in the block:
echo "Now in if"
This was the output:
Now lets try
tried
-bash: /Users/amorfis/.rvm/scripts/cd: line 14: syntax error near unexpected token `('
-bash: /Users/amorfis/.rvm/scripts/cd: line 14: ` cd() { __zsh_like_cd cd "$#" ; }'
So it looks like scm_breeze.sh is ok. The problem must be in .rvm, but only when scm_breeze.sh is run.
UPDATE 2:
The beginning of the .rvm/scripts/cd script looks like this:
#!/usr/bin/env bash
# Source a .rvmrc file in a directory after changing to it, if it exists. To
# disable this feature, set rvm_project_rvmrc=0 in /etc/rvmrc or $HOME/.rvmrc
case "${rvm_project_rvmrc:-1}" in
1|cd)
# clonned from git#github.com:mpapis/bash_zsh_support.git
source "$rvm_scripts_path/extras/bash_zsh_support/chpwd/function.sh"
# not using default loadign to support older Zsh
[[ -n "${ZSH_VERSION:-}" ]] &&
__rvm_version_compare "$ZSH_VERSION" -gt 4.3.4 ||
{
cd() { __zsh_like_cd cd "$#" ; }
popd() { __zsh_like_cd popd "$#" ; }
pushd() { __zsh_like_cd pushd "$#" ; }
}
I'd add this in as a comment, but I don't have the reputation to do so. I tried the answer from blob, but it didn't work.
I don't see the "scm_breeze-line", that Riaan Burger was talking about. Has anyone figured out an answer to this?
My error is pretty much the same:
/Users/myusername/.rvm/scripts/cd:14: defining function based on alias `cd' [ruby-2.3.3]
/Users/myusername/.rvm/scripts/cd:14: parse error near `()'
and line #14 says the same:
11 [[ -n "${ZSH_VERSION:-}" ]] &&
12 __rvm_version_compare "$ZSH_VERSION" -gt 4.3.4 ||
13 {
14 cd() { __zsh_like_cd cd "$#" ; }
15 popd() { __zsh_like_cd popd "$#" ; }
16 pushd() { __zsh_like_cd pushd "$#" ; }
17 }
I just ran into the same problem. The solution was to ensure the scm_breeze line executes after all the rvm ones.
Hit the same problem today, but the problem had nothing to do with scm_breeze in my case. If anyone stumbled onto this answer from google or some other place, maybe it'll help you.
Shortly after switching to OSX from Win7 I've been happily modifying anything and everything without necessarily understanding what I'm doing. Amongst other things, I've edited .bashrc as root (not the one from profile, rather the one located in /etc/.bashrc) and aliased cd like that:
alias cd='cd -P'
Never had problems with it before installing RVM, so if you were as root-happy as I once was, it might be worth checking whether you left yourself such a gift in the past.
I've moved said line into ~/.bash_profile and since then RVM happily runs without errors.
So basically what I did,
Step 1) Get a clone from SCM_BREEZE :-
git clone https://github.com/scmbreeze/scm_breeze.git
Step 2) Get a reference from Author's Doc (Link for Docs) and wrote few commands inside my local git repository's terminal,
. "$HOME/.scm_breeze/scm_breeze.sh"
update_scm_breeze
gs
It will update you scm breeze from github and patch your files if any
Your Git Status Command
N you are good to go...
Hope so it would help you now :)