Visual Studio Code to use node version specified by NVM

后端 未结 17 1226
渐次进展
渐次进展 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

    Particularly with the shell I had no problems, but you may:

    • check your shell is properly configure or change it (you might be using different shells for vscode or your terminal)
    • check your env and if it's not properly set use the terminal.integrated.env.

    I had issues with vscode itself and no solution could help me. So I finished using the following launch script.

        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/server.js",
            "runtimeExecutable": "/bin/bash",
            "runtimeArgs": ["-c", ". ~/.nvm/nvm.sh;nvm run default \"$@\"", "dummy"]
        },
    

    this assumes you have it configure for bash (otherwise change it to your shell) and you want to use the default node version as configured by nvm (you may also change it).

    Note: The "dummy" parameter is required so the rest of the parameters are properly parsed.

    A longer explanation of "dummy": Shell scripts use positional parameters where the first one will be the script location itself (addressed by $0), when using the -c flag the script is read inplace an there is no $0 being set. vscode will pass some arguments, like the node start script location which will be wrongly interpreted, so "dummy" pushes all parameters one spot. It can be just anything, but it must be there.

提交回复
热议问题