Skip to content

Instantly share code, notes, and snippets.

@sheraz-haider
Last active January 14, 2022 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sheraz-haider/e9762e9a92a3cd930f06f4dc8d3909c7 to your computer and use it in GitHub Desktop.
Save sheraz-haider/e9762e9a92a3cd930f06f4dc8d3909c7 to your computer and use it in GitHub Desktop.
Auto detect node version from .nvmrc file

Auto detect node version from .nvmrc file

This guide assumes nvm is already installed on your unix system and only works for bash

Open the ~/.bashrc file and find the following code

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

and replace it with this

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" --no-use  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion

if [ -f ".nvmrc" ]; then
  nvm use > /dev/null
else
  nvm use default > /dev/null
fi

Now go into the project root directory and create a .nvmrc file and add a node version you want to use like this

v12.22.1

Note: Node version you add in .nvmrc file must be installed with nvm first, like this nvm install 12.22.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment