ZNVM: Nodejs Version Manager for ZSH
January 7, 2025 ยท View on GitHub
Similar to nvm-sh but meant to be faster (on startup at least).
Features
- Install nodejs versions
- Use nodejs version defined in
.nvmrc,.znvmrcorDockerfile- To automatically use the version add
znvm hookwdchangeto your.zshrc
- To automatically use the version add
- Aliases for nodejs versions: for example
defaultcan be an alias forv12 - Support of closest version:
- if a certain version is not installed, it will try to find the closest version and will choose the newest one
for example if you try to use
v10.0.0but you don't have it installed, it will usev10.24.0if you have it installed
- if a certain version is not installed, it will try to find the closest version and will choose the newest one
for example if you try to use
- Autocompletion for znvm commands
- Load default nodejs version on shell startup
Install
Dependencies
You shell must be zsh
The following commands also needed for operation:
curlawktailheadcutsortuniq
General installation
git clone https://github.com/Ajnasz/znvm $HOME/src/znvm
Edit you .zshrc and the following line to enable the plugin:
# enable znvm plugin
. $HOME/src/znvm/znvm.plugin.zsh
# enable autocompletion for znvm
fpath+=$HOME/src/znvm
# load default nodejs version
# but only if it's not set (for example it will be set if you use tmux)
# remove the if statement if you want to make sure the default version used in a new shell
if ! znvm current > /dev/null
then
znvm use default
fi
# load version defined in .nvmrc
znvm hookwdchange
Install in oh-my-zsh
cd $ZSH
git submodule add https://github.com/Ajnasz/znvm custom/plugins/znvm
Install in zgenom
Add zgenom load Ajnasz/znvm in your .zshrc with your other zgenom load commands.
Enable the znvm plugin in your .zshrc.
How to enable plugins in oh-my-zsh
Load default nodejs version
# load default nodejs version
znvm use default
# load version defined in .nvmrc
znvm hookwdchange
Configuration
The ZNVM_DIR environment variable, default value is $HOME/.znvm
ZNVM_DIR="$HOME/.znvm"
The ZNVM_SEARCH_FILENAMES environment variable is a list of filenames (separated by space) znvm will look for whenever it executes the chpwd hook listener. Default value is .znvmrc .nvmrc Dockerfile
To disable Dockerfile read remove it from the list:
ZNVM_SEARCH_FILENAMES=".znvmrc .nvmrc"
Usage
Install a nodejs version
To install the latest nodejs v12:
znvm install v12
To install nodejs v8.1.1
znvm install v8.1.1
Create or update an alias
znvm alias default v12
Load a version
znvm use v12
Use a default version
znvm use default
Auto use from .znvmrc, .nvmrc or Dockerfile files
Add the following line to the .zshrc
znvm hookwdchange
The hook will traverse directory structure upwards from the current directory looking for the files defined in $ZNVM_SEARCH_FILENAMES every time you change directory.
That will add a hook, which executes every time you change directory. The hook listener will search .znvmrc, .nvmrc and Dockerfile in the directory you entered to. If no file found, it will try to find it in the parent directory until it reaches the root directory.
When it searches in a Dockerfile, it will try to extract the version number from FROM node: line.