Visual Studio Code to use node version specified by NVM

后端 未结 17 1183
渐次进展
渐次进展 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
    
    0 讨论(0)
  • 2020-12-07 17:01

    Some of the answers provided are correct and upvoted, but somewhat incomplete. This procedure worked for me:

    1. Open the terminal window inside VS Code and run node -v. You'll get for instance v10.12.0.
    2. Open a terminal window outside VS Code Change your node version with nvm (ie. nvm use v12.14.0)
    3. Cmd+ Shift + p and choose Preferences > Open Settings (JSON)
    4. Add "terminal.integrated.shellArgs.osx": [] to your user config
    5. Cmd+ Shift + p and choose Shell Command: Install 'code' command in PATH
    6. Close VS Code.
    7. Open a terminal window and run code. This will open VS Code with a new and updated bash / zsh session.
    8. Open the terminal window inside VS Code and run 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
    
    0 讨论(0)
  • 2020-12-07 17:02

    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"
    }
    
    0 讨论(0)
  • 2020-12-07 17:03

    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"
        }
    ]}
    
    0 讨论(0)
  • 2020-12-07 17:06

    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.

    0 讨论(0)
提交回复
热议问题