node.js Visual Studio Code 使用 NVM 指定的节点版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44700432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 18:18:27  来源:igfitidea点击:

Visual Studio Code to use node version specified by NVM

node.jsvisual-studio-codenvm

提问by Aseem Gautam

Is it possible for VS Code to use node version specified by NVM?

VS Code 是否可以使用 NVM 指定的节点版本?

I have 6.9.2 installed locally. Even after switching to another version, from the OS X terminal (not the VS Code terminal), restarting VS Code, VS Code still shows using 6.9.2.

我在本地安装了 6.9.2。即使在切换到另一个版本后,从 OS X 终端(不是 VS Code 终端),重新启动 VS Code,VS Code 仍然显示使用 6.9.2。

OS X terminal

OS X 终端

MacBook-Pro-3:~ mac$ node -v
v7.8.0

VS Code Terminal

VS 代码终端

MacBook-Pro-3:QB-Invoice-API mac$ node -v
v6.9.2

采纳答案by Aseem Gautam

The solution is to set alias default. In the OS terminal run -

解决方法是设置 alias default。在操作系统终端运行 -

nvm alias default 7.8.0

nvm alias default 7.8.0

Open vscode, now running node -vreturns 7.8.0

打开 vscode,现在运行node -v返回7.8.0

It seems vscode takes up this (alias default) value and not the node version that is set by nvm use X.X.X

似乎 vscode 占用了这个(别名默认)值,而不是由设置的节点版本 nvm use X.X.X

Restart VS code for it to pick up the changes.

重新启动 VS 代码以获取更改。

Update (12/04/2018)- This solution might not work for everyone. Please see below answers for other solutions.

更新 (12/04/2018)- 此解决方案可能不适用于所有人。有关其他解决方案,请参阅以下答案。

回答by Sm Srikanth

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)

在 VS Code 中,转到您的 launch.json 文件并在配置中添加 runtimeVersion 属性,如下所示。(在这个例子中,我们假设已经使用 nvm 安装了 4.8.7)

{
"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"
    }
]}

回答by Alongkorn Chetasumon

add runtimeExecutableto your .vscode/launch.jsonlike this

添加runtimeExecutable到你.vscode/launch.json喜欢这个

{
  "type": "node",
  "request": "launch",
  "name": "App",
  "program": "${workspaceRoot}/index.js",
  "runtimeExecutable": "${env:HOME}/.nvm/versions/node/v6.9.2/bin/node"
}

回答by Charlyboy

I had the same problem of being unable to keep my node version specified trough nvm in my OS X environment not only with VSCode but also with Atom Editor (using the platformio-ide-terminal package for managing the integrated terminal in it). None of the suggestions in the previous answers worked for me, besides me not using the debugger but using gulp and grunt for specific tasks. Apparently nvm does not get along with the integrated terminals or sub shells at least in these editors because when loading them the environment variable $PATH is modified internally and does the following according to a comment by one of the contributors of this package in this issue reported here NVM fails to load within nested shell #1652:

我遇到了同样的问题,即无法在 OS X 环境中通过 nvm 保持指定的节点版本,不仅使用 VSCode,而且使用 Atom 编辑器(使用 platformio-ide-terminal 包管理其中的集成终端)。除了我没有使用调试器而是将 gulp 和 grunt 用于特定任务之外,之前答案中的所有建议都不适合我。显然,至少在这些编辑器中,nvm 无法与集成终端或子 shell 相处,因为在加载它们时,环境变量 $PATH 在内部进行了修改,并根据此问题中该包的贡献者之一的评论执行以下操作这里NVM 无法在嵌套 shell #1652 中加载

" @charsleysaI know why nvm is throwing this error. In your subshell, somehow the /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin part of your PATH has been moved from the end of the PATH to the start.

  • When nvm is then started, it calls nvm_change_path (my contribution changed it to this from nvm_prepend_path), which modifies the nvm-relevant part of the path in place.
  • Nvm then checks the current npm prefix by asking npm what it is. Since /usr/local/bin/npm now has precendence, it reports /usr/local/bin.
  • Nvm then checks whether the current prefix as reported by npm is in the directory tree of the current nvm node version (at this stage, the installation directory of the node version your default nvm alias resolves to).
  • The prefix is not part of that tree, so it deactivates itself (calling nvm_strip_path in the process, which is why there's no nvm-related path in your subshell's PATH), and bails with the error you're getting. macOS's /etc/profile (or /etc/zprofile) calls /usr/libexec/path_helper, which does the PATH switcheroo.

In the parent shell, the PATH doesn't yet have an nvm dir in it, so by the time nvm runs, it prepends its directory to the path. But in the subshell, PATH has been reconfigured by macOS to put any non-system directories at the end and we have the problem."

" @charsleysa我知道为什么 nvm 会抛出这个错误。在你的子 shell 中,你的 PATH 的 /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin 部分已经从最后移开了PATH 到开始。

  • 当 nvm 启动时,它调用 nvm_change_path(我的贡献将它从 nvm_prepend_path 更改为这个),它修改了路径中与 nvm 相关的部分。
  • 然后 Nvm 通过询问 npm 是什么来检查当前的 npm 前缀。由于 /usr/local/bin/npm 现在具有优先权,它报告 /usr/local/bin。
  • 然后 Nvm 会检查 npm 报告的当前前缀是否在当前 nvm 节点版本的目录树中(在此阶段,您的默认 nvm 别名解析到的节点版本的安装目录)。
  • 该前缀不是该树的一部分,因此它会自行停用(在该过程中调用 nvm_strip_path,这就是为什么您的子 shell 的 PATH 中没有与 nvm 相关的路径的原因),并以您收到的错误为准。macOS 的 /etc/profile(或 /etc/zprofile)调用 /usr/libexec/path_helper,它执行 PATH switcheroo。

在父 shell 中,PATH 中还没有 nvm 目录,因此当 nvm 运行时,它会将其目录添加到路径中。但是在子 shell 中,macOS 重新配置了 PATH 以将任何非系统目录放在最后,我们遇到了问题。”

I was always getting this message when launching any integrated terminal:

启动任何集成终端时,我总是收到此消息:

nvm is not compatible with the npm config "prefix" option: currently set to "/usr/local" Run npm config delete prefixor nvm use --delete-prefix vx.x.x --silentto unset it.

nvm 与 npm config "prefix" 选项不兼容:当前设置为 "/usr/local" 运行npm config delete prefixnvm use --delete-prefix vx.x.x --silent取消设置。

What I did to solve this in my case was the "workaround" part of that same issue reported which is essentially the following:

在我的情况下,我为解决这个问题所做的是报告的同一问题的“解决方法”部分,其本质如下:

  • Reset the path by adding the following line inside my ~/.bash_profile at the very top before anything else: PATH="/usr/local/bin:$(getconf PATH)"
  • 通过在我的 ~/.bash_profile 最顶部添加以下行来重置路径:PATH="/usr/local/bin:$(getconf PATH)"

And after that no more warnings when I launch any integrated terminal on both editors and I can interact with nvm to switch between any node version easily and without problems at all.

之后,当我在两个编辑器上启动任何集成终端时不再出现警告,并且我可以与 nvm 交互以轻松且完全没有问题地在任何节点版本之间切换。

Here it is anotheralternative just in case this one doesn`t help that much.

这是另一种选择,以防万一这个方法没有多大帮助。

回答by Skeevs

I had the same problem, but the above answers didn't help.

我遇到了同样的问题,但上述答案没有帮助。

Apparently the default shellArgsfor osx are set to bashwhile I'm using zsh. I solved the problem by setting the shellArgsin my user settings to an empty array:

显然shellArgsbash当我使用zsh. 我通过将shellArgs用户设置中的 设置为空数组解决了这个问题:

"terminal.integrated.shellArgs.osx": []

"terminal.integrated.shellArgs.osx": []

回答by jr.

A alternative solution I've found is to simply launch code from the shell after you pick your node using nvm.

我发现的另一种解决方案是在使用 nvm 选择节点后简单地从 shell 启动代码。

You need to first open the command pallet and select "install 'code' into path".

您需要先打开命令托盘并选择“将'代码'安装到路径中”。

enter image description here

在此处输入图片说明

And then launch a terminal and select your node via nvm and then launch "code".

然后启动终端并通过 nvm 选择您的节点,然后启动“代码”。

enter image description here

在此处输入图片说明

回答by BlueTabasco

I am using oh-my-zshand it too was not using the node version specified by nvm. Tried several suggestions posted here, but the only way I managed to resolve this issue was by adding the following line to the top of ~/.zshrc

我正在使用oh-my-zsh,它也没有使用 nvm 指定的节点版本。尝试了此处发布的几个建议,但我设法解决此问题的唯一方法是将以下行添加到~/.zshrc

PATH="/usr/local/bin:$(getconf PATH)"

回答by estani

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

特别是对于外壳,我没有问题,但是您可能会:

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

我遇到了 vscode 本身的问题,没有任何解决方案可以帮助我。所以我完成了以下启动脚本的使用。

    {
        "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 defaultnode version as configured by nvm (you may also change it).

这假设您为 bash 配置了它(否则将其更改为您的 shell)并且您希望使用defaultnvm 配置的节点版本(您也可以更改它)。

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 -cflag the script is read inplace an there is no $0being 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.

对“虚拟”的更长解释:Shell 脚本使用位置参数,其中第一个将是脚本位置本身(由 寻址$0),当使用-c标志时,脚本被原地读取并且没有$0被设置。vscode 将传递一些参数,例如会被错误解释的节点启动脚本位置,因此“dummy”将所有参数推到一个位置。它可以是任何东西,但它必须在那里。

回答by madflanderz

I tried all the suggested solutions but nothing was working.

我尝试了所有建议的解决方案,但没有任何效果。

/usr/local/bin/node was pointing to somewhere. i made a symlink to a specific nvm node folder and that was solving the issue for me:

/usr/local/bin/node 指向某个地方。我创建了一个指向特定 nvm 节点文件夹的符号链接,这为我解决了这个问题:

ln -s /Users/mad/.nvm/versions/node/v11.1.0/bin/node /usr/local/bin/node

回答by franziga

I have the same problem and I found that I have nodeinstalled by brewand nvm. I uninstalled nodeinstalled by brewand the versions on both terminal and visual studio code are the same now.

我有同样的问题,我发现我已经node安装了brewnvm。我卸载了node安装,brew终端和visual studio代码上的版本现在是一样的。