我对nodejs完全不熟悉。我试图让nodejs在我的Windows 2008盒子上工作,以安装Karma,我将用于TDDing我的AngularJs代码。到目前为止,我已经完成了以下步骤

Install using Chocolatey ==> npm is not recognised Install using 64-bit nodejs installer from nodejs.org ==> npm is not recognised At this stage, running where npm gives me c:\User\<Username>\AppData\Roaming\npm which has nothing in it I figure out that nodejs is installed in C:\Program Files\nodejs. Opening a command prompt in this directory makes npm work fine. So I added C:\Program Files\nodejs to PATH only to get the same error again that npm is not recognized 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 I do see a Node.js icon in my Start -> Programms mennu which takes me to nodejs console but not sure what to do with that.

在这个过程中,我是否错过了任何重要的步骤?

Edit

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

Edit

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


当前回答

如果你正在使用VS Code,关闭VS Code,然后重新打开。

我尝试关闭航站楼,然后打开新的航站楼,但它没有工作。

重新启动VS代码工作!

其他回答

为了详细说明布雷诺的回答……对于Windows 7,这些步骤对我来说是有效的:

Open the Control Panel (Click the Start button, then click Control Panel) Click User Accounts Click Change my environment variables Select PATH and click the Edit... button At the end of the Variable value, add ;C:\Program Files\nodejs Click Ok on the "Edit User Variable" window, then click Ok on the "Environment Variables" window Start a command prompt window (Start button, then type cmd into the search and hit enter) At the prompt (C:\>) type npm and hit enter; you should now see some help text (Usage: npm <command> etc.) rather than "npm is not recognized..."

现在你可以开始使用npm了!

通过命令查看npm config:

npm config list

它需要属性:“prefix”,全局“prefix”和“node bin location”。

; userconfig C:\Users\username\.npmrc
cache = "C:\\ProgramData\\npm-cache"
msvs_version = "2015"
prefix = "C:\\ProgramData\\npm"
python = "C:\\Python27\\"
registry = "http://registry.com/api/npm/npm-packages/"

; globalconfig C:\ProgramData\npm\etc\npmrc
cache = "C:\\ProgramData\\npm-cache"
prefix = "C:\\ProgramData\\npm"

; node bin location = C:\Program Files\nodejs\node.exe
; cwd = C:\WINDOWS\system32

在这种情况下,它需要将这些路径添加到环境变量PATH的末尾:

;C:\Program Files\nodejs;C:\ProgramData\npm;

问题出在系统策略上。我已经尝试了以下评论在权力脚跟,然后它开始工作

$> Set-ExecutionPolicy Unrestricted -Scope CurrentUser -Force 
$> npm install -g npm-windows-upgrade 
$> npm-windows-upgrade

我从下面的链接得到这个想法

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

我也遇到过这个问题。事实证明,Windows不喜欢在命令行上使用单引号。罪魁祸首是我的一个npm脚本。我把单引号改成了转义双引号:

'npm -s run sass-build'

to

\"npm -s run sass-build\"