6

I have two projects with different version of node, which are managed with nvm:

  1. Project 1: node v6.10.4
  2. Project 2: node v8.9.4

On each project's root folder there is a file .nvmrc with its node version. When I open the integrated terminal on VS Code the default node version is selected (other than the previous two) and I need to type $ nvm use to change to the correct version of node each project uses. Is there a way to execute automatically $ nvm use after terminal opens, or another way to achieve my objective? Sometimes I open the terminal and forget to execute the command.

I'm using Ubuntu 16.04, VS Code 1.20.1 and nvm 0.33.8.

Thanks.

1 Answer 1

19

I've found a solution:

To work with nvm, I added in ~/.bashrc (from nvm instructions):

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

Now I've changed it to:

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

The --no-use option is to tell nvm "not load default node bin dir on PATH", because it will be done later on the last lines: if .nvmrc exists on the directory where the integrated terminal opens, the version inside .nvmrc will be loaded, default version otherwise.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.