我对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安装添加了这句令人不安的引用,但我只是不确定是哪一个。


当前回答

为了详细说明布雷诺的回答……对于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了!

其他回答

安装node后,不要忘记重新启动计算机!这句话难倒了我。

如果软件包已成功安装,但仍显示“‘npm’未被识别为内部或外部命令、可操作程序或批处理文件。”

单击windows启动按钮。 寻找“ALL APPS”,你会看到Node.js和Node.js命令提示符。 你可以以管理员身份运行Node.js命令提示符,一旦它运行,它就会显示一条消息“你的环境已经为使用Node.js 6.3.0 (x64)和npm设置好了。”

然后它就开始工作了…

你不需要“安装”Node.js。只需从https://nodejs.org/en/download/releases/下载软件包并在某处提取内容。然后,在命令提示符中,你可以使用cd导航到那里,然后你可以从那个位置运行npm。虽然在运行npm之前必须执行cd C:\Users\YourAcct\Documents\node-v16.13.0 win-x64或类似命令,但这是一种将它放到计算机上并运行npm的方法,而不必成为管理员或编辑环境变量。

好吧,在我的情况下,通过Mocha做测试,我尝试了一切,只是意识到我必须在package.json内的测试用例脚本标记周围删除单引号。

我在所有*.test.js文件上运行mocha测试用例,如下所示:

package.json

之前:

 "scripts": {
    "test": "mocha server/**/*.test.js",
    "test-watch": "nodemon --exec 'npm run test'"
  }

After(删除单引号- npm run test):

 "scripts": {
    "test": "mocha server/**/*.test.js",
    "test-watch": "nodemon --exec npm run test"
  }

对我有用,以防别人也被困在这事上。

我按照AngularJS教程安装了nodejs。npm命令工作时,我打开一个新的cmd窗口,但不是在当前的。 所以解决办法是关闭并打开一个新的cmd窗口。