Visual Studio Code to use node version specified by NVM

后端 未结 17 1232
渐次进展
渐次进展 2020-12-07 16:01

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

17条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-07 16:57

    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
    

提交回复
热议问题