250

我正在进入一个 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,设置了变量,然后重新启动,仍然是错误。

关于我缺少什么的任何线索?

4

26 回答 26

504

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

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

然后安装软件包:

npm install --global node-gyp

安装后,您将下载所有 node-gyp 依赖项,但您仍然需要环境变量。验证 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,并验证变量是否存在,通过set PYTHON它应该返回变量($env:PYTHON如果使用 Powershell)

最后重新申请npm install <module>

于 2016-09-22T20:43:18.127 回答
142

你的问题是你没有设置环境变量。

该错误清楚地说明了这一点:

gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.

在您的评论中,您说您这样做了:

set PYTHONPATH=%PYTHONPATH%;C:\My_python_lib

这很好,但这并没有设置PYTHON变量,它设置了PYTHONPATH变量。


同时,仅使用该set命令只会影响当前cmd会话。cmd如果您在那之后重新启动,正如您所说的那样,您最终会得到一个没有设置该变量的全新会话。

有几种方法可以永久设置环境变量——最简单的是在 XP 的系统控制面板中,这在 Vista 中当然不同,在 7 中再次不同,在 8 中再次不同,但你可以 google 为它。

或者,只需set在命令之前执行正确的操作npm,而无需在其间重新启动。


您可以通过执行配置脚本尝试执行的完全相同的操作来测试您是否做对了:在运行之前npm,尝试运行%PYTHON%。如果你做对了,你会得到一个 Python 解释器(你可以立即退出)。如果你得到一个错误,你没有做对。


这有两个问题:

set PYTHON=%PYTHON%;D:\Python

首先,您设置PYTHON;D:\Python. 额外的分号适用于以分号分隔的路径列表,例如PATHor PYTHONPATH,但不适用于单个值,例如PYTHON. 同样,当您想向路径列表添加另一个路径时,向现有值添加一个新值是您想要的,而不是单个值。所以,你只想要set PYTHON=D:\Python.

其次,D:\Python不是您的 Python 解释器的路径。类似于D:\Python\Python.exe, 或D:\Python\bin\Python.exe. 找到正确的路径,确保它可以自己运行(例如,键入D:\Python\bin\Python.exe并确保您获得了 Python 解释器),然后设置变量并使用它。


所以:

set PYTHON=D:\Python\bin\Python.exe

或者,如果您想使其永久化,请在控制面板中执行等效操作。

于 2013-02-28T02:00:39.937 回答
23

对我来说,在安装 windows-build-tools 后使用以下评论

npm --add-python-to-path='true' --debug install --global windows-build-tools

运行下面的代码

npm config set python "%USERPROFILE%\.windows-build-tools\python27\python.exe"

已经奏效了。

于 2018-12-06T13:02:07.187 回答
14

这是一个为我解决了很多这些问题的指南。

http://www.steveworkman.com/node-js/2012/installing-jsdom-on-windows/

我记得特别重要的是 python 版本。确保安装 2.7.3 而不是 3。

于 2013-02-28T02:52:42.303 回答
11

其中一个和/或多个应该有助于:

  1. 添加C:\Python27\到您的PATH变量(考虑到您在此目录中安装了 Python)
    如何设置环境PATH变量:http
    ://www.computerhope.com/issues/ch000549.htm 设置变量后重新启动控制台和/或 Windows。

  2. 在与上面相同的部分(“环境变量”)中,添加具有名称PYTHON和值C:\Python27\python.exe
    的新变量 设置变量后重新启动控制台和/或 Windows。

  3. 在管理员模式下打开 Windows 命令行 ( cmd) 。 将目录更改为您的 Python 安装路径: 使某些安装需要符号链接:
    cd C:\Python27
    mklink python2.7.exe python.exe

请注意,您应该使用 Python 2.x 而不是 3.x来运行node-gyp基于安装的!

下面的文字是关于 Unix 的,但 Windows 版本也需要 Python 2.x:

You can install with npm:

$ npm install -g node-gyp
You will also need to install:

On Unix:
python (v2.7 recommended, v3.x.x is not supported)
make
A proper C/C++ compiler toolchain, like GCC

这篇文章也可能有帮助:https ://github.com/nodejs/node-gyp#installation

于 2016-09-05T13:19:22.850 回答
8

我有同样的问题,这些答案都没有帮助。在我的情况下,PYTHON 变量设置正确。然而python安装得太深,即路径太长。所以,我做了以下事情:

  1. 将 python 重新安装到 c:\python
  2. 将环境变量 PYTHON 设置为 C:\python\python.exe

就是这样!

于 2015-07-07T13:30:34.447 回答
7

这有帮助:https ://www.npmjs.com/package/node-gyp

我按照以下步骤操作:

npm install -g node-gyp

然后:

npm install --global --production windows-build-tools
于 2018-10-10T14:39:43.037 回答
7

有一些解决方案可以解决这个问题:1)以“管理员”身份运行命令提示符。

如果第一个解决方案不能解决您的问题,请尝试以下解决方案:

2)以管理员身份打开命令提示符,粘贴以下代码行并回车:

npm install --global --production windows-build-tools
于 2018-02-11T07:32:41.710 回答
6

我在尝试安装 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。

于 2019-07-08T08:44:20.443 回答
6

这是让 NPM 为您做所有事情的最简单方法

npm --add-python-to-path='true' --debug install --global windows-build-tools
于 2019-10-23T16:24:42.677 回答
6

以下内容以管理员身份从命令行为我工作:

安装 windows-build-tools(这可能需要 15-20 分钟):

 npm --add-python-to-path='true' --debug install --global windows-build-tools

添加/更新环境变量:

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

安装节点gyp:

npm install --global node-gyp

将 exe 文件的名称从 Python 更改为 Python2.7。

C:\Users\username\.windows-build-tools\python27\Python2.7

npm install module_name --save

于 2018-02-09T05:36:27.967 回答
5

TL;DR 使用名称 python2.7.exe 复制您的 python.exe 或别名

我的 python 2.7 安装为

D:\app\Python27\python.exe

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

吉普错误!堆栈错误:找不到 Python 可执行文件“python2.7”,您可以设置 PYTHON 环境变量。
吉普错误!堆栈在 failNoPython (C:\Program Files\nodejs\node_modules\npm\node_modules\node-gyp\lib\configure.js:103:14)

原因是在 node-gyp 的 configure.js 中,python 可执行文件被解析为:

var python = gyp.opts.python || process.env.PYTHON || 'python'

事实证明,gyp.opts.python 的值为“python2.7”,因此覆盖了 process.env.PYTHON。

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

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

您需要管理员权限才能执行此操作。

于 2015-09-01T11:08:46.183 回答
5

我不禁要提到这一点。如果您使用的是 Python3 并且使用 node-gyp 失败,那么我很遗憾地告诉您 node-gyp 目前不支持 python3。

这是一个链接: https ://github.com/nodejs/node-gyp/issues/1268 https://github.com/nodejs/node-gyp/issues/193

于 2018-03-10T01:57:14.640 回答
4

正确的方法是 1) 从这里下载并安装 python 2.7.14 。2)从这里为python设置环境变量。

完毕!

注意:请相应地设置环境变量。我在这里回答了windows。

于 2018-03-16T02:55:14.503 回答
3

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

1-以管理员身份运行此 cmd:

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

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

于 2019-01-22T15:13:22.743 回答
2

为什么不在这里下载 python 安装程序?当您检查路径安装时,它会为您工作

于 2017-10-15T14:59:58.283 回答
1

如果您尝试在 Cygwin 上使用它,那么您需要按照答案中的说明进行操作。(Cygwin 如何处理 Windows 符号链接是一个问题。)

于 2016-08-24T16:12:44.343 回答
1

吉普错误!配置错误gyp ERR!堆栈错误:找不到 Python 可执行文件“python”,您可以设置 PYT HON 环境变量。

不需要重新安装,这个异常由 node-gyp 脚本抛出,然后尝试重建。这是足够的设置环境变量,就像我所做的那样:

SET PYTHON=C:\work\_env\Python27\python.exe
于 2016-08-16T15:16:52.480 回答
1

我尝试了上述所有步骤,没有一个对我有用。我试图在 Windows 10 系统上通过 yarn 安装 argon2 npm 包。

所以这就是最终奏效的方法

  1. npm --add-python-to-path='true' --debug install --global windows-build-tools在命令终端中以管理员身份运行 python 安装,上面的命令遇到了一个无限循环,您可以使用 Ctrl + C 终止该循环。
  2. 我通过运行这个设置环境变量 setx PYTHON "%USERPROFILE%\.windows-build-tools\python27\python.exe"
  3. 我通过运行此命令全局安装了 node-gyp
    npm install --global node-gyp
  4. 我下载了 VS 2019 社区版安装程序,在安装时我添加了使用 C++ 的桌面开发,如图所示 在此处输入图像描述
  5. 我再次运行了 yarn 命令。例如yarn add argon2

我希望这些步骤对您有所帮助。我花了几个小时才找到这个解决方案

于 2021-06-16T10:11:53.043 回答
1

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

我有同样的问题,我只是这样解决了这个问题。

正如其他一些人指出的那样,这是可变配置,它仅适用于当前的 cmd 会话,并且(显然)您必须在运行 npm install 之前设置路径。

我希望这有帮助。

于 2016-05-27T00:43:45.400 回答
1

示例:pg_config 不可执行 / 错误 node-gyp

解决方案:在 Windows 上尝试添加 PATH Env -> C:\Program Files\PostgreSQL\12\bin

为我工作,现在我可以使用 npm i pg-promise 或其他依赖项。

于 2019-11-12T13:17:55.253 回答
0

对我来说,问题是我使用的是 node 的最新版本,而不是LTS稳定版本并推荐给大多数用户的版本。
使用LTS版本解决了这个问题。
您可以从这里下载:

LTS 版本

当前最新版本

于 2019-06-08T13:41:05.370 回答
0

在下载 Akveo ngx-admin 模板后尝试执行“npm install”时,我遇到了同样的错误。

这是一种奇怪的情况,因为我只是在另一台笔记本电脑上启动了相同的模板。我意识到我安装了另一个版本的 npm。

在此处输入图像描述

如您所见,我的节点版本是“14.17.0”,第一步我的 npm 版本是“7.13.0”。然后我意识到我另一台笔记本电脑的npm版本是“6.14.11”,所以我决定通过执行“npm install -g npm@6.14.11”来降级npm版本。

然后“npm install”工作得很好。

注意:无需安装 windows-build-tools 或任何 python 版本。

于 2021-05-16T19:58:47.947 回答
0

什么对我有用:

  • 这里安装需要的python版本
  • 使用 cmd 运行以下命令npm config set python C:\Users\jorge\AppData\Local\Programs\Python\Python310\python.exe

更改目录的路径

于 2021-10-25T20:20:32.680 回答
0

设置如下路径,它将起作用

> set PYTHON=D:\\ranjith\\installed\\python-3.6.4\\python.exe

> npm config set python D:\\ranjith\\installed\\python-3.6.4\\python.exe

然后构建你的项目(对我来说)

> yarn build
于 2020-07-23T07:46:39.693 回答
-1

我的问题的答案很简单。

在 Python 安装期间,我打开了一个 Windows 命令窗口。

安装 Python 后,我尝试从该窗口启动 Python。

对我来说解决方案很简单。

当我关闭命令窗口并打开一个新的命令窗口时,问题就消失了。

我必须打开一个新的命令窗口,以便路径环境变量包含 Python 的路径。

于 2021-07-20T22:25:48.120 回答