When running an NPM script from the script explorer, it uses NVM's default version of node. Instead, I'd like to use the version dictated by .nvmrc. Is that possible?
2 Answers
There are several extensions that run nvm use on each terminal opened if an .nvmrc file is found in the workspace. These work for the NPM Script Explorer too.
The most downloaded one is called vsc-nvm.
In case it does not work out of the box, note that it runs nvm use in a /bin/sh shell, therefore you need to make sure nvm is accessible and works as intended from within sh.
P.S. This sounds like a good feature to add to the NPM Script Explorer itself, but as far as I can see, nobody suggested it yet.
Comments
This works form me in linux.
Prerequisites
have nvm installed
have a
.nvmrcfile with the project's Node version placed in the root folder of your project
In your VS Code settings.json file, add these definitions
"terminal.integrated.profiles.linux": {
"NvmBash": {
"path": "bash",
"source": "bash",
"extensionIdentifier": "linux",
"args": [
"-c",
"[ -f .nvmrc ] && { source ~/.nvm/nvm.sh; nvm use $(cat .nvmrc) || nvm install $(cat .nvmrc); } || true; exec $@"
]
}
},
"terminal.integrated.defaultProfile.linux": "NvmBash"