Is it possible for VS Code to use node version specified by NVM?
I have 6.9.2 installed locally. Even after switching to another version, from the OS X terminal (no
I found that setting the node version locally in a sub shell before calling code works well, while not changing the version in the current shell, e.g.
$ (nvm use 14; code .)
Therefore, to make it work transparently for any project folder, create a file .precode
in the project folder with shell commands to source before starting code - e.g.,
nvm use 14
Then add to ~/.bashrc
pre_code(){
if [ $# == 1 ] && [ -f ${1}/.precode ] ; then
echo "(source ${1}/.precode ; `which code` ${@})"
(source ${1}/.precode ; `which code` ${@})
else
`which code` ${@}
fi
}
alias code='pre_code'
(Note: Run source ~/.bashrc
in any shell opened before the edit in order for the edit to take effect.)
Then, assuming the necessary file ~/myproject/.precode
exists, starting code with
$ code ~/myproject
will result in some diagnostic output on the shell, e.g.
source github/myproject/.precode
Now using node v14.15.1 (npm v6.14.8)
as well launching a new vscode window with the correct node version in the terminal window and debugger. However, in the shell from which it was launched, the original node version remains, e.g.
$ node -v
v12.19.1
Some of the answers provided are correct and upvoted, but somewhat incomplete. This procedure worked for me:
node -v
. You'll get for instance v10.12.0
.nvm use v12.14.0
)Cmd
+ Shift
+ p
and choose Preferences > Open Settings (JSON)"terminal.integrated.shellArgs.osx": []
to your user configCmd
+ Shift
+ p
and choose Shell Command: Install 'code' command in PATHcode
. This will open VS Code with a new and updated bash
/ zsh
session.node -v
. You'll get v12.14.0
.Bonus: If you always want to get a particular node version on VS Code's terminal, set it as default by opening a terminal window outside VS Code and running:
nvm alias default v12.14.0
add runtimeExecutable
to your .vscode/launch.json
like this
{
"type": "node",
"request": "launch",
"name": "App",
"program": "${workspaceRoot}/index.js",
"runtimeExecutable": "${env:HOME}/.nvm/versions/node/v6.9.2/bin/node"
}
In VS Code, go to your launch.json file and add the runtimeVersion attribute inside configurations, as shown below. (In this example, we are assuming 4.8.7 is already installed using nvm)
{
"version": "<some-version>",
"configurations": [
{
"type": "node",
"runtimeVersion": "4.8.7", // If i need to run node 4.8.7
"request": "launch",
"name": "Launch",
"program": "${workspaceFolder}/sample.js"
}
]}
Did not tried all of the solution, but for me updating nvm simply worked.
Just follow the installation here and make sure that you bash_profile
is updated.