node.js 'npm' 不是内部或外部命令,也不是可运行的程序或批处理文件

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

'npm' is not recognized as internal or external command, operable program or batch file

windowsnode.js

提问by Suhas

I am completely new to nodejs. I am trying to get nodejs to work on my Windows 2008 box in order to install Karma which I would use for TDDing my AngularJs code. I have done the following steps so far

我对 nodejs 完全陌生。我试图让 nodejs 在我的 Windows 2008 机器上工作,以便安装 Karma,我将用它来 TDDing 我的 AngularJs 代码。到目前为止,我已经完成了以下步骤

  1. Install using Chocolatey ==> npm is not recognised
  2. Install using 64-bit nodejs installer from nodejs.org ==> npm is not recognised
  3. At this stage, running where npmgives me c:\User\<Username>\AppData\Roaming\npmwhich has nothing in it
  4. I figure out that nodejs is installed in C:\Program Files\nodejs. Opening a command prompt in this directory makes npmwork fine.
  5. So I added C:\Program Files\nodejsto PATHonly to get the same error again that npm is not recognized
  6. One of the github issues on nodejs repository says that I need to restart the machine and it would fix. But that has not helped so far
  7. I do see a Node.jsicon in my Start -> Programmsmennu which takes me to nodejs console but not sure what to do with that.
  1. 使用 Chocolatey ==> npm 无法识别安装
  2. 使用来自 nodejs.org 的 64 位 nodejs 安装程序进行安装 ==> 无法识别 npm
  3. 在这个阶段,跑步where npm给了我c:\User\<Username>\AppData\Roaming\npm什么都没有的
  4. 我发现 nodejs 安装在C:\Program Files\nodejs. 在此目录中打开命令提示符可以npm正常工作。
  5. 所以我添加C:\Program Files\nodejsPATH只是为了再次得到同样的错误npm is not recognized
  6. nodejs 存储库上的 github 问题之一说我需要重新启动机器,它会修复。但这到目前为止还没有帮助
  7. 我确实Node.js在我的Start -> Programms菜单中看到了一个图标,它将我带到 nodejs 控制台,但不确定如何处理它。

Have I missed any important step in the process?

我错过了这个过程中的任何重要步骤吗?

Edit

编辑

I figured out that if I open "Nodejs command prompt" from program files, then npm is recognized. How do I make it work on a normal command prompt?

我发现如果我从程序文件中打开“Nodejs 命令提示符”,那么 npm 就会被识别。我如何使它在正常的命令提示符下工作?

Edit

编辑

After nodeI started facing a similar problem with another application. I posted this questionon superuser and as rightly pointed out by the accepted answer, I had an additional quote in my PATHwhich was causing issues with all the paths added after the quote. I have a feeling that some Chocolatey install adds this troubling quote but I am just not sure which one.

node我开始面临另一个应用程序的类似问题之后。我在超级用户上发布了这个问题,并且正如接受的答案正确指出的那样,我在我的中有一个额外的引用,PATH这导致引用后添加的所有路径出现问题。我有一种感觉,一些 Chocolatey 安装添加了这个令人不安的引用,但我不确定是哪一个。

回答by Breno

Just add:

只需添加:

;C:\Program Files\nodejs\

To the end of your Pathvariable on the "User variable"section of the Environment Variables on the System Properties.

到系统属性上环境变量的“用户变量”部分的Path变量的末尾。

After that, reopen your command prompt and type

之后,重新打开命令提示符并键入

npm

This should work.

这应该有效。

回答by user3414680

Don't forget to reboot your computer after installing node! That one got me.

安装节点后不要忘记重新启动计算机!那一个得到了我。

回答by Luke

To elaborate on Breno's answer... For Windows7 these steps worked for me:

详细说明布雷诺的回答......对于Windows7,这些步骤对我有用:

  1. Open the Control Panel (Click the Start button, then click Control Panel)
  2. Click User Accounts
  3. Click Change my environment variables
  4. Select PATHand click the Edit...button
  5. At the end of the Variable value, add ;C:\Program Files\nodejs
  6. Click Okon the "Edit User Variable" window, then click Okon the "Environment Variables" window
  7. Start a command prompt window (Start button, then type cmdinto the search and hit enter)
  8. At the prompt (C:\>) type npmand hit enter; you should now see some help text (Usage: npm <command>etc.) rather than "npm is not recognized..."
  1. 打开控制面板(单击开始按钮,然后单击控制面板
  2. 单击用户帐户
  3. 单击更改我的环境变量
  4. 选择PATH并单击编辑...按钮
  5. 在变量值的末尾,添加 ;C:\Program Files\nodejs
  6. 单击确定在“编辑用户变量”窗口,然后单击确定的“环境变量”窗口
  7. 启动命令提示符窗口(开始按钮,然后在搜索中键入cmd并按 Enter)
  8. 在提示符 ( C:\>) 输入npm并按回车键;您现在应该会看到一些帮助文本(Usage: npm <command>等),而不是“无法识别 npm...

Now you can start using npm!

现在你可以开始使用 npm 了

回答by Muhammad Faizan Khan

For Windowsusers: A picture tells a thousand words enter image description here

对于Windows用户:一张图片说明一千个字 在此处输入图片说明

Refrence: npm not recognized as internal or external command

参考:npm 不被识别为内部或外部命令

回答by Raul

Don't forget to run cmd as admin.

不要忘记以管理员身份运行 cmd。

回答by gbgnv

I had the same problem described by Ashu, but in addition to that, the PATH entry for nodejs was terminated by a backslash:

我遇到了Ashu描述的相同问题,但除此之外,nodejs 的 PATH 条目被反斜杠终止:

C:\Program Files\nodejs\

I also had to remove that final backslash in order to have it work.

我还必须删除最后的反斜杠才能使其工作。

回答by Ashu

I faced the exact same issue and notice that after installing node.js there was a new path entry in the user variable section for PATH with value --> c:\User\\AppData\Roaming\npm. Also the Path entry in the system variable is appended with --> C:\Program Files\nodejs. Now since user variable has preference over system you have two options to fix this. Either delete the path from user variable or correct the right path (C:\Program Files\nodejs). Restart CMD and it should work.

我遇到了完全相同的问题,并注意到在安装 node.js 后,PATH 的用户变量部分中有一个新的路径条目,其值为 --> c:\User\\AppData\Roaming\npm。此外,系统变量中的 Path 条目附加有 --> C:\Program Files\nodejs。现在,由于用户变量优先于系统,因此您有两个选项可以解决此问题。从用户变量中删除路径或更正正确的路径 (C:\Program Files\nodejs)。重新启动 CMD,它应该可以工作。

回答by Udhav Sarvaiya

Just Download and Install Node.js from here https://nodejs.org/en/

只需从这里下载并安装 Node.js https://nodejs.org/en/

If you run the downloaded file and install it, they will automatically configure for your system

如果您运行下载的文件并安装它,它们将自动为您的系统配置

You don't need any other configurations anymore, now you can use the npmcommand anywhere

您不再需要任何其他配置,现在您可以在任何地方使用npm命令



If the Nodejs is successfully installed and still displays the message like this:

如果 Nodejs 安装成功并且仍然显示如下消息:

'npm' is not recognized as an internal or external command, operable program or batch file.

'npm' 不是内部或外部命令,也不是可运行的程序或批处理文件。

Follow the steps below for Windows users:

对于 Windows 用户,请按照以下步骤操作:

  1. Go to My ComputerProperties
  2. Click Advanced System Settingfrom the Left bar of a window.
  3. Now you have a System Properties window. Click Advanced
  4. Then, Click Environment Variablebutton
  5. Now you have Environment variable window: From System Variable, Select Path
  6. Click Edit
  7. At the end of the Variable value, add ;C:\Program Files\nodejs\

    Note: If you have installed nodejson other drives then please act accordingly.

  8. Click Okall the open dialogue box
  1. 转到我的电脑属性
  2. 单击窗口左侧栏中的高级系统设置
  3. 现在您有一个系统属性窗口。单击高级
  4. 然后,单击环境变量按钮
  5. 现在你有环境变量窗口:从系统变量,选择路径
  6. 单击编辑
  7. 在变量值的末尾,添加 ;C:\Program Files\nodejs\

    注意:如果您在其他驱动器上安装了nodejs,请采取相应措施。

  8. 单击确定所有打开的对话框

Very important Note: "Close your Command Prompt And Restart Again"(It's very important because if you didn't restart your command prompt then changes will not be reflected.)

非常重要的注意事项“关闭您的命令提示符并重新启动”(这非常重要,因为如果您没有重新启动命令提示符,则不会反映更改。)

Now you can use the npmcommand anywhere

现在你可以在任何地方使用npm命令

回答by codemirror

If you are using VS Code, close VS code and open again.

如果您使用的是 VS Code,请关闭 VS Code 并再次打开。

I tried closing Terminal and then opening new Terminal but it didn't work.

我尝试关闭终端,然后打开新的终端,但没有用。

Re-Starting VS Code works!

重新启动 VS Code 有效!

回答by rash.tay

You might have already received a response but this might help others since I experienced the same issue recently and this is what I did:

您可能已经收到回复,但这可能对其他人有所帮助,因为我最近遇到了同样的问题,这就是我所做的:

  1. Provided a path entry to powershell. For me the path was C:\Windows\System32\WindowsPowerShell\v1.0
  2. Then I opened cmd prompt with administrative privileges and copied this

    @powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin

  1. 提供了 powershell 的路径条目。对我来说路径是C:\Windows\System32\WindowsPowerShell\v1.0
  2. 然后我用管理权限打开 cmd 提示符并复制这个

    @powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString(' https://chocolatey.org/install.ps1'))" && SET PATH=%PATH%;%ALLUSERSPROFILE% \巧克力\bin

into the cmd prompt.

进入cmd提示符。

  1. Next, choco install nodejs
  2. Restart and everything worked fine. Try opening cmd without admin privileges and run npm -v Cheers.
  1. 接下来,choco 安装 nodejs
  2. 重新启动,一切正常。尝试在没有管理员权限的情况下打开 cmd 并运行 npm -v Cheers。