4ndt3s
4ndt3s

Reputation: 3467

How to automatically activate specific node version when VS Code integrated terminal opens?

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.

Upvotes: 6

Views: 2987

Answers (1)

4ndt3s
4ndt3s

Reputation: 3467

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.

Upvotes: 16

Related Questions