我正在进入一个Node.js代码库,这需要我通过NPM下载一些依赖项,即jQuery。

在尝试运行npm install jquery时,我一直得到这个错误:

Your environment has been set up for using Node.js 0.8.21 (x64) and NPM

C:\Users\Matt Cashatt>npm install jquery
npm http GET https://registry.npmjs.org/jquery
npm http 304 https://registry.npmjs.org/jquery
npm http GET https://registry.npmjs.org/jsdom
npm http GET https://registry.npmjs.org/xmlhttprequest
npm http GET https://registry.npmjs.org/htmlparser/1.7.6
npm http GET https://registry.npmjs.org/location/0.0.1
npm http GET https://registry.npmjs.org/navigator
npm http GET https://registry.npmjs.org/contextify
npm http 304 https://registry.npmjs.org/htmlparser/1.7.6
npm http 304 https://registry.npmjs.org/xmlhttprequest
npm http 304 https://registry.npmjs.org/location/0.0.1
npm http 304 https://registry.npmjs.org/navigator
npm http 304 https://registry.npmjs.org/jsdom
npm http 304 https://registry.npmjs.org/contextify
npm http GET https://registry.npmjs.org/bindings
npm http GET https://registry.npmjs.org/cssom
npm http GET https://registry.npmjs.org/cssstyle
npm http GET https://registry.npmjs.org/request
npm http 304 https://registry.npmjs.org/bindings

> contextify@0.1.4 install C:\Users\Matt Cashatt\node_modules\jquery\node_module
s\contextify
> node-gyp rebuild


C:\Users\Matt Cashatt\node_modules\jquery\node_modules\contextify>node "C:\Progr
am Files\nodejs\node_modules\npm\bin\node-gyp-bin\\..\..\node_modules\node-gyp\b
in\node-gyp.js" rebuild
npm http 304 https://registry.npmjs.org/cssstyle
npm http 304 https://registry.npmjs.org/cssom
npm http 304 https://registry.npmjs.org/request
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYT
HON env variable.
gyp ERR! stack     at failNoPython (C:\Program Files\nodejs\node_modules\npm\nod
e_modules\node-gyp\lib\configure.js:113:14)
gyp ERR! stack     at C:\Program Files\nodejs\node_modules\npm\node_modules\node
-gyp\lib\configure.js:82:11
gyp ERR! stack     at Object.oncomplete (fs.js:297:15)
gyp ERR! System Windows_NT 6.1.7601
gyp ERR! command "node" "C:\\Program Files\\nodejs\\node_modules\\npm\\node_modu
les\\node-gyp\\bin\\node-gyp.js" "rebuild"
gyp ERR! cwd C:\Users\Matt Cashatt\node_modules\jquery\node_modules\contextify
gyp ERR! node -v v0.8.21
gyp ERR! node-gyp -v v0.8.4
gyp ERR! not ok
npm ERR! error rolling back Error: ENOTEMPTY, rmdir 'C:\Users\Matt Cashatt\node_
modules\jquery\node_modules\jsdom\node_modules\request\tests'
npm ERR! error rolling back  jquery@1.8.3 { [Error: ENOTEMPTY, rmdir 'C:\Users\M
att Cashatt\node_modules\jquery\node_modules\jsdom\node_modules\request\tests']
npm ERR! error rolling back   errno: 53,
npm ERR! error rolling back   code: 'ENOTEMPTY',
npm ERR! error rolling back   path: 'C:\\Users\\Matt Cashatt\\node_modules\\jque
ry\\node_modules\\jsdom\\node_modules\\request\\tests' }
npm ERR! contextify@0.1.4 install: `node-gyp rebuild`
npm ERR! `cmd "/c" "node-gyp rebuild"` failed with 1
npm ERR!
npm ERR! Failed at the contextify@0.1.4 install script.
npm ERR! This is most likely a problem with the contextify package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node-gyp rebuild
npm ERR! You can get their info via:
npm ERR!     npm owner ls contextify
npm ERR! There is likely additional logging output above.

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "jquery"
npm ERR! cwd C:\Users\Matt Cashatt
npm ERR! node -v v0.8.21
npm ERR! npm -v 1.2.11
npm ERR! code ELIFECYCLE
npm ERR! Error: ENOENT, lstat 'C:\Users\Matt Cashatt\node_modules\jquery\node_mo
dules\jsdom\node_modules\request\tests\test-pipes.js'
npm ERR! If you need help, you may report this log at:
npm ERR!     <http://github.com/isaacs/npm/issues>
npm ERR! or email it to:
npm ERR!     <npm-@googlegroups.com>

npm ERR! System Windows_NT 6.1.7601
npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod
ejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "jquery"
npm ERR! cwd C:\Users\Matt Cashatt
npm ERR! node -v v0.8.21
npm ERR! npm -v 1.2.11
npm ERR! path C:\Users\Matt Cashatt\node_modules\jquery\node_modules\jsdom\node_
modules\request\tests\test-pipes.js
npm ERR! fstream_path C:\Users\Matt Cashatt\node_modules\jquery\node_modules\jsd
om\node_modules\request\tests\test-pipes.js
npm ERR! fstream_type File
npm ERR! fstream_class FileWriter
npm ERR! code ENOENT
npm ERR! errno 34
npm ERR! fstream_stack C:\Program Files\nodejs\node_modules\npm\node_modules\fst
ream\lib\writer.js:284:26
npm ERR! fstream_stack Object.oncomplete (fs.js:297:15)
npm ERR!
npm ERR! Additional logging details can be found in:
npm ERR!     C:\Users\Matt Cashatt\npm-debug.log
npm ERR! not ok code 0

C:\Users\Matt Cashatt>

看起来失败是由于缺少Python安装。好吧,我已经安装了Python,设置了变量,然后重新启动,仍然出现错误。

你知道我错过了什么吗?


当前回答

如果你没有安装python以及所有node-gyp依赖项,只需使用管理员权限打开Powershell或Git Bash并执行:

npm install --global --production windows-build-tools

然后安装包:

npm install --global node-gyp

安装完成后,您将下载所有node-gyp依赖项,但仍然需要环境变量。在正确的文件夹中确实找到了Validate Python:

C:\Users\ben\.windows-build-tools\python27\python.exe 

*注意-它使用python 2.7而不是3。X,因为不支持*

如果它没有呻吟,继续创建你的(用户)环境变量:

setx PYTHON "%USERPROFILE%\.windows-build-tools\python27\python.exe"

重新启动cmd,并通过设置PYTHON来验证变量是否存在,应该返回该变量(如果使用Powershell,则为$env:PYTHON)

最后,重新应用npm install <module>

其他回答

为你的python.exe创建一个名为python2.7.exe的副本或别名

我的python 2.7安装为D:\app\Python27\python.exe

无论我如何设置(并验证)PYTHON env变量,我总是得到这个错误:

gyp ERR! stack Error: Can't find Python executable "python2.7", you can set the PYTHON env variable.
gyp ERR! stack     at failNoPython (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:103:14)

原因是在node-gyp的config .js中,python可执行文件是这样解析的:

var python = gypp .opt .python || process.env.PYTHON || 'python'

结果发现,gypp .opt .python有值'python2.7',因此重写process.env.PYTHON。

我通过为node-gyp正在寻找的python.exe可执行文件创建一个别名来解决这个问题:

D:\app\Python27>mklink python2.7.exe python.exe

该操作需要管理员权限。

下面是正确的命令:set path=%path%;C:\Python34[替换为正确的python安装路径]

我遇到过同样的问题,我就这样解决了。

正如其他人指出的那样,这是一个易变的配置,它只对当前的cmd会话有效,并且(显然)你必须在运行npm install之前设置路径。

我希望这能有所帮助。

对我来说,这些步骤解决了问题:

1-以管理员身份运行命令:

NPM install -global -production windows-build-tools

2-然后在第一步完成后运行npm重建(特别是完成python 2.7安装,这是问题的主要原因)

如果你没有安装python以及所有node-gyp依赖项,只需使用管理员权限打开Powershell或Git Bash并执行:

npm install --global --production windows-build-tools

然后安装包:

npm install --global node-gyp

安装完成后,您将下载所有node-gyp依赖项,但仍然需要环境变量。在正确的文件夹中确实找到了Validate Python:

C:\Users\ben\.windows-build-tools\python27\python.exe 

*注意-它使用python 2.7而不是3。X,因为不支持*

如果它没有呻吟,继续创建你的(用户)环境变量:

setx PYTHON "%USERPROFILE%\.windows-build-tools\python27\python.exe"

重新启动cmd,并通过设置PYTHON来验证变量是否存在,应该返回该变量(如果使用Powershell,则为$env:PYTHON)

最后,重新应用npm install <module>

在尝试安装node-sass@4.9.4时,我遇到了同样的挑战。

在查看了当前的官方文档并阅读了上面的答案后,我注意到您可能不一定要安装node-gyp或安装windows-build工具。这是关于在windows上安装node-gyp的。记住node-gyp涉及到node-sass的安装过程。你真的不需要重新安装另一个python版本。

这是救星,配置“npm”在安装任何需要构建工具的包时应该寻找的python路径。

C:\> npm config set python /Python36/python

我在windows-7上安装了python3.6.3。