在 Windows 10 上安装 Node.js(和 npm)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27344045/
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:11:57  来源:igfitidea点击:

Installing Node.js (and npm) on Windows 10

node.jswindowsnpminstallation

提问by Stephan Bijzitter

I had some issues trying to install Node on Windows 10 and found the solution.

我在尝试在 Windows 10 上安装 Node 时遇到了一些问题并找到了解决方案。

The error was as follows:

错误如下:

C:\Users\Stephan>npm
Error: ENOENT, stat 'C:\Users\Stephan\AppData\Roaming\npm'

C:\Users\Stephan>npm
错误:ENOENT,stat 'C:\Users\Stephan\AppData\Roaming\npm'

The solution is below.

解决方法如下。

回答by Stephan Bijzitter

Edit:It seems like new installers do not have this problem anymore, see this answerby Parag Meshramas my answer is likely obsolete now.

编辑:似乎新安装程序不再有这个问题,请参阅Parag Meshram 的这个答案,因为我的答案现在可能已经过时了。

Original answer:

原答案:

Follow these steps, closely:

严格按照以下步骤操作:

  • http://nodejs.org/download/download the 64 bits version, 32 is for hipsters
  • Install it anywhere you want, by default: C:\Program Files\nodejs
  • Control Panel -> System -> Advanced system settings -> Environment Variables
  • Select PATHand choose to edit it.
  • http://nodejs.org/download/下载64位版本,32位适合潮人
  • 默认情况下,将其安装在您想要的任何位置: C:\Program Files\nodejs
  • 控制面板 -> 系统 -> 高级系统设置 -> 环境变量
  • 选择PATH并选择对其进行编辑。

If the PATH variable is empty, change it to this: C:\Users\{YOUR USERNAME HERE}\AppData\Roaming\npm;C:\Program Files\nodejs

如果 PATH 变量是empty,请将其更改为:C:\Users\{YOUR USERNAME HERE}\AppData\Roaming\npm;C:\Program Files\nodejs

If the PATH variable already contains C:\Users\{YOUR USERNAME HERE}\AppData\Roaming\npm, append the following right after: ;C:\Program Files\nodejs

如果 PATH 变量已经包含C:\Users\{YOUR USERNAME HERE}\AppData\Roaming\npm,请在后面添加以下内容:;C:\Program Files\nodejs

If the PATH variable contains information, but nothing regarding npm, append this to the end of the PATH: ;C:\Users\{YOUR USERNAME HERE}\AppData\Roaming\npm;C:\Program Files\nodejs

如果 PATH 变量包含信息,但没有关于 npm 的信息,请将其附加到 PATH 的末尾: ;C:\Users\{YOUR USERNAME HERE}\AppData\Roaming\npm;C:\Program Files\nodejs

Now that the PATH variable is set correctly, you will still encounter errors. Manually go into the AppData directory and you will find that there is no npm directory inside Roaming. Manually create this directory.

现在 PATH 变量设置正确,您仍然会遇到错误。手动进入AppData目录,会发现Roaming里面没有npm目录。手动创建此目录。

Re-start the command prompt and npm will now work.

重新启动命令提示符,npm 现在就可以工作了。

回答by foreyez

go to http://nodejs.org/

http://nodejs.org/

and hit the button that says "Download For ..."

然后点击“下载...”按钮

This'll download the .msi (or .pkg for mac) which will do all the installation and paths for you, unlike the selected answer.

这将下载 .msi(或 .pkg for mac),它将为您完成所有安装和路径,与所选答案不同。

回答by Dunken

In addition to the answer from @StephanBijzitter I would use the following PATHvariables instead:

除了@StephanBijzitter 的回答之外,我还会使用以下PATH变量:

%appdata%\npm
%ProgramFiles%\nodejs

So your new PATHwould look like:

所以你的新PATH看起来像:

[existing stuff];%appdata%\npm;%ProgramFiles%\nodejs

This has the advantage of neiter being user dependent nor 32/64bit dependent.

这具有既不依赖于用户也不依赖于 32/64 位的优点。

回答by Parag Meshram

New installers (.msi downloaded from https://nodejs.org) have "Add to PATH" option. By default it is selected. Make sure that you leave it checked.

新安装程序(从https://nodejs.org下载的 .msi )具有“添加到 PATH”选项。默认情况下它被选中。确保选中它。

Add to PATH

添加到路径

回答by Tom

Everything should be installed in %appdata% (C:\Users\\AppData\Roaming), not 'program files'.

一切都应安装在 %appdata% (C:\Users\\AppData\Roaming) 中,而不是“程序文件”中。

Here's why...

这就是为什么...

The default MSI installer puts Node and the NPM that comes with it in 'program files' and adds this to the system path, but it sets the user path for NPM to %appdata% (c:\users[username]\appdata\roaming) since the user doesn't have sufficient priveleges to write to 'program files'.

默认的 MSI 安装程序将 Node 和它附带的 NPM 放在“程序文件”中并将其添加到系统路径中,但它将 NPM 的用户路径设置为 %appdata% (c:\users[username]\appdata\roaming ) 因为用户没有足够的权限来写入“程序文件”。

This creates a mess as all modules go into %appdata%, and when you upgrade NPM itself - which NPM themselves recommend you do right away - you end up with two copies: the original still in 'program files' since NPM can't erase that, and the new one inn %appdata%.

当所有模块都进入 %appdata% 时,这会造成混乱,并且当您升级 NPM 本身时(NPM 自己建议您立即这样做),您最终会得到两个副本:原始文件仍在“程序文件”中,因为 NPM 无法擦除那个,还有新的一家客栈 %appdata%。

Even worse, if you mistakenly perform NPM operations as admin (much easier on Windows then on *nix) then it will operate on the 'program files' copy of NPM node_modules. Potentially a real mess.

更糟糕的是,如果您错误地以管理员身份执行 NPM 操作(在 Windows 上比在 *nix 上容易得多),那么它将对 NPM node_modules 的“程序文件”副本进行操作。可能真的一团糟。

So, when you run the installer simply point it to %appdata% and avoid all this.

因此,当您运行安装程序时,只需将其指向 %appdata% 并避免所有这些。

And note that this isn't anything wierd - it's what would happen if you ran the installer with just user priveleges.

请注意,这没什么奇怪的 - 如果您仅使用用户权限运行安装程序,就会发生这种情况。

回答by Adeel Imran

I had the same problem, what helped we was turning of my anti virus protection for like 10 minutes while node installed and it worked like a charm.

我遇到了同样的问题,是什么帮助我们在安装 node 时关闭了我的防病毒保护大约 10 分钟,并且它像魅力一样工作。

回答by arunram

You should run the installer as administrator.

您应该以管理员身份运行安装程序。

  1. Run the command prompt as administrator
  2. cd directory where msi file is present
  3. launch msi file by typing the name in the command prompt
  4. You should be happy to see all node commands work from new command prompt shell
  1. 以管理员身份运行命令提示符
  2. msi 文件所在的 cd 目录
  3. 通过在命令提示符中键入名称来启动 msi 文件
  4. 您应该很高兴看到所有节点命令都在新的命令提示符 shell 中工作

回答by Joel Chu

The reason why you have to modify the AppData could be:

您必须修改 AppData 的原因可能是:

  1. Node.js couldn't handle path longer then 256 characters, windows tend to have very long PATH.
  2. If you are login from a corporate environment, your AppData might be on the server - that won't work. The npm directory must be in your local drive.
  1. Node.js 无法处理超过 256 个字符的路径,Windows 往往有很长的 PATH。
  2. 如果您从公司环境登录,您的 AppData 可能在服务器上 - 这将不起作用。npm 目录必须在您的本地驱动器中。

Even after doing that, the latest LTE (4.4.4) still have problem with Windows 10, it worked for a little while then whenever I try to:

即使这样做之后,最新的 LTE (4.4.4) 仍然在 Windows 10 上存在问题,每当我尝试时,它都会工作一段时间:

$ npm install _some_package_ --global 

Node throw the "FATAL ERROR CALL_AND_RETRY_LAST Allocation failed - process out of memory" error. Still try to find a solution to that problem.

节点抛出“致命错误 CALL_AND_RETRY_LAST 分配失败 - 进程内存不足”错误。仍然尝试找到解决该问题的方法。

The only thing I find works is to run Vagrant or Virtual box, then run the Linux command line (must matching the path) which is quite a messy solution.

我发现唯一有效的方法是运行 Vagrant 或 Virtual box,然后运行 ​​Linux 命令行(必须匹配路径),这是一个相当混乱的解决方案。