【问题标题】:Node.js: Python not found exception due to node-sass and node-gypNode.js:由于 node-sass 和 node-gyp,Python 未找到异常
【发布时间】:2018-01-29 18:50:39
【问题描述】:

突然在我的一个 jenkins 环境中构建开始失败,而在本地机器上它似乎工作正常,因为我安装了 python,

从日志中我能够检测到问题出在我研究时来自 node-sass v3.8.0node-gyp v3.5.0 的内部依赖性通过访问this关于node-gyp,发现需要安装Python的先决条件。

所以我的问题是,我可以安装什么版本的 node-sass 来绕过这个问题,或者是否有更好的解决方案,因为直到今天早上我的构建在同一环境中运行良好。

节点 v5.10.1

错误日志

gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2
gyp verb `which` failed     at getNotFoundError (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:80:29)
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:89:16
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\isexe\index.js:44:5
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\isexe\windows.js:29:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:82:15)
gyp verb `which` failed  python2 { [Error: not found: python2] code: 'ENOENT' }
gyp verb check python checking for Python executable "python" in the PATH
gyp verb `which` failed Error: not found: python
gyp verb `which` failed     at getNotFoundError (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:13:12)
gyp verb `which` failed     at F (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:68:19)
gyp verb `which` failed     at E (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:80:29)
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\which\which.js:89:16
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\isexe\index.js:44:5
gyp verb `which` failed     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\isexe\windows.js:29:5
gyp verb `which` failed     at FSReqWrap.oncomplete (fs.js:82:15)
gyp verb `which` failed  python { [Error: not found: python] code: 'ENOENT' }
gyp verb could not find "python". checking python launcher 
gyp verb could not find "python". guessing location 
gyp verb ensuring that file exists: C:\Python27\python.exe
gyp ERR! configure error 
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable.
gyp ERR! stack     at Object.failNoPython (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\node-gyp\lib\configure.js:454:19)
gyp ERR! stack     at Object.<anonymous> (C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\node-gyp\lib\configure.js:480:16)
gyp ERR! stack     at C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\graceful-fs\polyfills.js:284:29
gyp ERR! stack     at FSReqWrap.oncomplete (fs.js:82:15)
gyp ERR! System Windows_NT 6.3.9600
gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files (x86)\\Jenkins\\jobs\\NdbSite-hot-fix-Manual-PreBuild\\workspace\\src\\NdbSite.UI\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
gyp ERR! cwd C:\Program Files (x86)\Jenkins\jobs\NdbSite-hot-fix-Manual-PreBuild\workspace\src\NdbSite.UI\node_modules\node-sass
gyp ERR! node -v v5.10.1
gyp ERR! node-gyp -v v3.5.0
gyp ERR! not ok 
Build failed

非常感谢任何想法,谢谢。

【问题讨论】:

  • 你想安装angular,所以你还需要angular-cli工具,那么你需要python2,在windows上,你还需要visualstudio2017附带的msbuild.exe。因此,您首先需要安装 Visual Studio 安装程序工具。然后你安装了这么多工具,你需要在 npm 中配置工具的版本。

标签: node.js node-gyp node-sass


【解决方案1】:

两个选项(2021 年 12 月):

  • 1 - 如果您无法升级 node-sass 包!
    • 安装 Python 2 并修改其 .exe 文件名。
    • 修复PATH env 变量记录顺序(详情如下)。
  • 2 - 更新 node-sass 包。

前提条件

您在 Windows 10 上将 Node.JS 更新到了最新版本 (v16.13.1)。
您在项目文件夹中运行 yarn installnpm install 并收到错误消息(如下所示)。

选项 1. 安装 Python-2 并修复 PATH env

See MSI installer

安装后

注意! 如果在 x64 上出现以下错误,请尝试将最新的 Node.js 从 x64 重新安装到 x86。 (但对于 x86,您也可能会收到内存限制错误。)

Error: Node Sass does not yet support your current environment: Windows 64-bit with Unsupported runtime (93)

PATH env 变量中对 Python 记录进行排序

Python 3 记录必须高于 Python 2 记录!

复制python.exepython2.exe

选项 2. 升级 node-sass

超过package.json文件

修改package.json 并在您的项目文件夹上运行yarn installnpm install
其他文件会自动更新。

之前

更新后:

或者使用 yarn 或 npm CLI 来做同样的事情,但它可能会明显改变您的 .lock 文件。

更新后你会发现你还需要更新sass-loaderwebpack等。

您可能遇到的错误(仅供参考)

gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2

gyp ERR! stack Error: Command failed: C:\Python310\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack   File "<string>", line 1
gyp ERR! stack     import sys; print "%s.%s.%s" % sys.version_info[:3];
gyp ERR! stack                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
gyp ERR! stack SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?

【讨论】:

  • 感谢这工作!但另外我将“python.exe”复制到“python2.exe”,所以python2在搜索路径中。
【解决方案2】:

您需要按照以下步骤修复错误:

    1: DELETE node_modules
    2: npm install node-sass@<version-as-per-below-table>
    3. npm install

每个 node-sass 版本都对应一个特定的 Node.JS 构建。

以下是 node-sass (Check updates to below table here) 支持的最低和最高支持版本的快速指南:


| NodeJS  | Supported node-sass version | Node Module |
|---------|-----------------------------|-------------|
| Node 16 | 6.0+                        | 93          |
| Node 15 | 5.0+                        | 88          |
| Node 14 | 4.14+                       | 83          |
| Node 13 | 4.13+, <5.0                 | 79          |
| Node 12 | 4.12+                       | 72          |
| Node 11 | 4.10+, <5.0                 | 67          |
| Node 10 | 4.9+, <6.0                  | 64          |
| Node 8  | 4.5.3+, <5.0                | 57          |
| Node <8 | <5.0                        | <57         |

【讨论】:

  • 调整版本号解决了运行后问题npm install
  • 你根本不应该推荐 node-sass。它已被弃用。现在要走的路是使用 dart sass 编译器(npm 'sass' 模块)
  • 你是天才!我忘了检查我的 package.json 中的 node-sass 版本。对于仍在苦苦挣扎的其他人-检查您的所有版本!就我而言,安装了 node v16.13、python 3.10、node-gyp 8.3,但我忘记将 node-sass 的版本更改为 6.0.1。
  • 对我来说,将节点版本从 16 替换为 14 后,此错误消失了。
  • 这对我有用,谢谢!
【解决方案3】:

所以这发生在我最近在 Windows 上。我使用具有管理员权限的 PowerShell 按照以下步骤修复它:

  1. 删除node_modules文件夹
  2. 以管理权限运行npm install --global windows-build-tools。 (在我的情况下需要重新启动 - 并且无需询问即可重新启动!!!)
  3. npm install重新安装节点模块或节点sass

【讨论】:

  • 这正是正确的答案。 npm install --global windows-build-tools 安装 Python 2.7,并在全局范围内安装它(因此您不必在每个项目的基础上重新安装)。它修复了“无法安装 SASS”的问题。注意:在 Windows 上,您必须以“管理员”身份运行 npm 命令提示符。
  • 它对我有用,Windows 10,节点 v10.15.0,npm 6.7.0
  • 我还必须删除 package-lock.json 才能使其正常工作。
  • 如果您尝试此答案并且在运行npm 时它会冻结,请尝试使用windows-build-tools v4: npm install --global --production windows-build-tools@4.0.0,如here 所述。
  • 这对我有用,但我还必须将我的 node-sass 版本更新为 5.0.0
【解决方案4】:

我们在 2021 年设置新的 TeamCity 构建代理时遇到了这个问题。最初我们使用 Chocolatey 安装了 nodejs。我们重新安装回到原来的安装程序node-v12.12.0-x64.msi。安装程序对话框中的第三个屏幕提供安装编译本机模块所需的工具:

这样做之后,我们的问题就消失了。

【讨论】:

  • 我知道node-v12.12.0 已经过时了,但我们正在尝试匹配其他构建代理机器。
  • 我刚刚在最近的 NodeJS 16.6.2 安装中选中了上面的复选框。我仍然有这个问题。
【解决方案5】:

几天前我遇到了同样的问题。相信我,他们不需要在您的项目或系统中安装或卸载其他依赖项。

我是如何解决这个问题的,让我分享一下我的经验。

  • 请转到package.json文件
  • 一直向下滚动或搜索optionalDependencies Object
  • 只需删除整个对象并保存文件
  • 尝试 npm install,它会起作用的

另一种可能的解决方案:

如果 optionalDepedencies 对象中存在 node-sass 元素,请检查您的 package.json 文件,然后您需要以这种方式运行 npm 命令npm install --no-optional 或如果只是从 optionalDepedencies Object

中删除 node-sass 元素

注意:此解决方案仅适用于 Angular 项目

enter image description here 请查看 package.json 文件的截图

【讨论】:

    【解决方案6】:

    如果您使用的是 Visual Studio。请按以下步骤操作:

    npm cache clean --force
    npm config set msvs_version 2019 --global
    npm install node-sass
    

    【讨论】:

      【解决方案7】:

      你也可以尝试使用 sass 模块而不是 node-sass:

      npm install sass
      

      【讨论】:

        【解决方案8】:

        这是帮助我在运行基于旧的 node-gyp v3.8.0 的项目时防止出现错误 Error: not found: python2 的原因。

        1. 运行npm install --global --production windows-build-tools@4.0.0
        2. %USERPROFILE%\.windows-build-tools\python27 中将python.exe 重命名为python2.exe
        3. 确保%USERPROFILE%\.windows-build-tools\python27 是您的PATH 环境变量的一部分(这是windows-build-tools 应该已经为您做的)
        4. 下载Visual Studio 2017 Build Tools
        5. 告诉npm 使用VS 2017 和npm config set msvs_version 2017
        6. 运行使用node-gyp的脚本(node-gyp现在会找到python2.exe

        替代方案:强制升级 node-gyp

        您也可以尝试升级您的项目以使用node-gyp v8.0.0 而不是node-gyp v3.8.0。在这种情况下,请将以下 resolution 条目添加到您的 package.json

          "resolutions": {
            "node-gyp": "8.0.0"
          }
        

        然后,确保您已安装 Python 3Visual Studio 2017 构建工具 和/或 Visual Studio 2019 构建工具。你也可以运行npm config delete msvs_versionnode-gyp自动检测你的VS版本。

        测试:

        • Windows 10 专业版 19042.985
        • Node.js v15.14.0
        • npm v7.7.6
        • node-gyp -v v3.8.0.

        【讨论】:

          【解决方案9】:

          Python2 维护状态为“生命终结”,因此默认情况下它不会安装在新的 ubuntu 版本上。

          安装最新的 python2.7 对我来说效果很好。

          以下是安装说明:

          tar -xf Python-2.7.18.tgz
          cd Python-2.7.18
          ./configure --enable-optimizations
          sudo make altinstall
          

          打开一个新终端并尝试再次输入npm install

          【讨论】:

            【解决方案10】:

            对于 yarn 项目,使用“yarn install --ignore-engines”忽略节点版本将安装任何 node-sass v. 而不会出现这些 python 错误

            【讨论】:

              【解决方案11】:

              我有 node 15.x.x"node-sass": "^4.11.0"。 我在node-sass 的发行说明中看到了与node-sass 4.11.0 兼容的node 最高版本是11,所以我卸载了node 并重新安装了11.15.0 版本(我正在使用Windows)。 检查node-sass releases(this is what you should see in the node-sass releases.)

              希望对我的英语有所帮助和抱歉:)

              【讨论】:

              • 非常感谢,切换到节点 11.15.0 成功了。除此之外,我还卸载了我的 python 版本 3 并切换到了 python 2。不确定两者是否都能正常工作
              • 这是我的解决方案,谢谢! (我使用 nvm 所以我只是切换到旧版本的节点)
              【解决方案12】:

              嘿,我按照以下步骤解决了这个错误

              • 首先我卸载了python 3.8.6(最新版本)
              • 然后我安装了 python 2.7.1 (任何 Python 2 版本都可以,但不会太旧,建议使用)
              • 然后我将c:\python27 添加到环境变量中
              • 我的操作系统是 windows,所以我关注了这个link
              • 成功了

              【讨论】:

              • 只有在您不需要 Python3 并且没有依赖它的应用程序的情况下,这才是一个解决方案。
              • 是的,它是正确的,我认为你不会对 python 2.7.1 有任何问题。
              • 在 2.7 和 3.xx 之间存在重大变化,这并不是真正的解决方案。
              • 在 C:\Python27 中将 python.exe 重命名为 python2.exe 对我有用
              【解决方案13】:

              我在 Windows 10 上发现了与 Node 12.19.0yarn 1.22.5 相同的问题。我通过安装最新的稳定 python 64 位并添加python安装期间环境变量的路径。 python安装后,我为环境变量重新启动了我的机器。

              【讨论】:

              • @RohanDevaki 似乎您一直在建议进行大量编辑,将随机关键字格式化为代码。请停下。例如,“Windows 10”、“Node 12.19.0”、“Python 64-bit”等不是代码,不应该这样格式化。由版主查看this post
              • @DonaldDuck,好吧,对不起,我不会重复这个。
              【解决方案14】:

              node-gyp 需要旧的 Python 2 - link

              如果您没有安装它 - 请查看有关安装 windows-build-tools 的其他答案。

              如果您像我一样,并且同时安装了新旧 Python 版本,那么 node-gyp 可能会尝试使用 Python 3。这会导致以下 SyntaxError: invalid syntax 错误。 p>

              我发现一篇关于安装两个 Python 版本的文章。他们建议将 Python 2.* 可执行文件重命名为 python2.exe - link

              所以看起来 node-gyp 期望找到重命名的旧 Python 2 可执行文件。因此错误消息:

              ...
              gyp verb check python checking for Python executable "python2" in the PATH
              gyp verb `which` failed Error: not found: python2
              ...
              

              一旦我将C:\Python27\python.exe 重命名为C:\Python27\python2.exe,它就可以正常工作。

              当然,C:\Python27\C:\Python39\ 都必须在 PATH 变量中。并且不需要在 npm config 中设置旧的 Python 版本。您的默认 Python 仍将是新的。

              【讨论】:

              • @RohanDevaki 您的答案完全不同,因为您建议删除 Python 3。虽然我的建议是安装两个 Python 版本并重命名 Python 2 可执行文件。
              • @ford04 "Python"、"error message" 等不是代码。您应该拒绝该编辑。见meta.stackoverflow.com/q/360491/4284627
              • 它救了我的命。
              【解决方案15】:

              错误消息意味着它无法找到您的 python 可执行文件或二进制文件。

              在许多情况下,它安装在 c:\python27。 如果还没有安装,可以用npm install --global windows-build-tools安装,只有在还没有安装的情况下才有效。

              将其添加到环境变量中并不总是有效。 更好的选择是在 npm 配置中设置它。

              npm config set python c:\python27\python.exe

              【讨论】:

              • 在 npm config 中设置 python 对我来说是一个很好的解决方案。
              • 这正是我可以轻松解决的方法。
              • 请停止使用python2。当前版本的python是3.9。这与很久以前修复的这个错误有关:fixed bug in node-gyp :github.com/nodejs/node-gyp/issues/1977 => 升级你的 node-sass 版本将升级 node-gyp 版本然后修复错误(如果你有最新的pyhon)
              • 谢谢 - 这正是问题所在。太糟糕了,我花了两天时间才找到您的简单解决方案
              • 更新 07/2021 :最好的解决方案,我已经尝试了上述所有答案,但只有这个对我有用。
              【解决方案16】:

              我的回答可能并不适用于所有人。 节点版本:v10.16.0 NPM:6.9.0

              我在使用 node-sassnode-sass-middleware 时遇到了很多麻烦。它们是有趣的包,因为它们被广泛使用(每周数百万次下载),但它们的 github 显示出有限的依赖关系和覆盖范围。我正在更新我一直在开发的旧平台。

              我最终不得不做的是:

              1)手动Delete node_modules

              2)手动Delete package-lock.json

              3) sudo npm install node-sass --unsafe-perm=true --allow-root

              4) sudo npm install node-sass-middleware --unsafe-perm=true --allow-root

              我得到了以下帮助,谢谢!

              Pre-built binaries not found for grpc@1.10.1 and node@10.9.0

              Error: EACCES: permission denied when trying to install ESLint using npm

              【讨论】:

              • 我执行了所有 4 个步骤但没有解决,然后执行这个解决的 npm i -D node-sass
              • 您可能使用了 @Arun 提到的其他 Node 版本等
              【解决方案17】:

              我的机器是 Windows 10,我在尝试使用 node-sass 包编译 SASS 时遇到了类似的问题。我的节点版本是v10.16.3,npm版本是6.9.0

              我解决问题的方法:

              1. 首先删除package-lock.json文件和node_modules/文件夹。
              2. 以管理员身份打开 Windows PowerShell。
              3. 运行命令npm i -g node-sass
              4. 之后,进入项目文件夹并运行npm install
              5. 最后,运行 SASS 编译脚本,在我的例子中是npm run build:css

              而且有效!!

              【讨论】:

              • npm install --global windows-build-tools 擦除 package-lock.json 和 npm i -g node-sass 解决了我的问题;
              • 如果您的组织的做法是不进行全球安装,则不是可接受的解决方案。
              • 对于 Windows,请在 stackoverflow.com/a/40552409/508907 找到 node_modules
              【解决方案18】:

              这已经 2 岁了,但他们都没有帮助我。

              我卸载了我的 NodeJS v12.8.1 (Current) 并安装了一个全新的 v10.16.3 (LTS),我的 ng build --prod 工作正常。

              【讨论】:

                【解决方案19】:

                查看所有答案后,我注意到此解决方案可能非常有用。它在安装 node-sass 时解释了how to configure "npm" to find your installed python version。请记住,node-sass 需要 node-gyp(一个 npm 构建工具)来查找您的 python 路径。或者只是安装 python,并按照给出的相同解决方案(在链接中)。谢谢。

                【讨论】:

                  【解决方案20】:

                  我不得不:

                  Delete node_modules
                  Uninstall/reinstall node
                  npm install node-sass@4.12.0
                  

                  根据据说与正确节点一起工作的版本,强制它到正确的 sass 版本后工作正常。

                  NodeJS  Minimum node-sass version   Node Module
                  Node 12 4.12+   72
                  Node 11 4.10+   67
                  Node 10 4.9+    64
                  Node 8  4.5.3+  57
                  

                  还有很多其他错误似乎是由错误的 sass 版本定义引起的。

                  【讨论】:

                  • 谢谢!对我来说就是这样,一位同事使用的是较旧的 node 版本,我使用的是 12,所以它需要不同版本的 node-sass。我将 node-sass 更新到 4.12,并强迫我的同事升级到 node 12,一切都很好!
                  • 这几乎概括了这个问题。 Node-sass 版本与特定版本的 nodejs 兼容。这真的应该有一个主要的版本来说明这一点。任何低于 4.12 的版本都不适用于节点 12.14
                  • 现在没有版本与node 14 on Windows x64 一起使用。我必须降级到节点 12 才能使其工作。
                  • 只是一个更新:在此处检查兼容性:npmjs.com/package/node-sass 并根据您的节点版本安装 node-sass。此外,如果问题仍然存在,请运行 npm rebuild node-sass
                  【解决方案21】:

                  尝试在我的 PC 上安装不同版本的 python 时遇到同样的问题。只需将node升级到最新版本v8.11.2和npm 5.6.0,然后安装node-sass@4.5.3就可以了。

                  【讨论】:

                  • 这对我有用,我一直在为此寻找几个小时!提示:如果你想处理多个版本的 Node.js,使用 NVM 真的很有帮助。
                  【解决方案22】:

                  Node-sass 在安装时会尝试为您的平台下载二进制文件。 3.8 https://github.com/sass/node-sass/releases/tag/v3.8.0 支持节点 5 如果您的 Jenkins 无法下载预构建的二进制文件,那么您需要遵循 Node-gyp README 上的平台要求(Python2、VS 或 MSBuild,...) 如果可能的话,我建议您将 Node 更新到至少 6,因为 Node 不再支持 5。 如果要升级到 8,则需要将 node-sass 更新到 4.5.3

                  【讨论】:

                  • 非常感谢您的努力,我能够找到 node-modules 中缺少 node-sass 包的问题,​​检查并发现确实是版本问题,但我的是由于连接超时,reset-cache 并且它现在似乎工作正常,但是是的将根据您的修复更新节点。
                  • @nschonni 我也面临同样的错误。节点版本为 7.7.1,npm 为 4.1.2。你能帮我解决这个问题吗?
                  • Node (10.4.0)、npm (6.1.0)、node-sass (4.9.0) 的相同问题
                  • 这是否意味着我需要降级我的节点?我有 node 12,需要安装 node-sass 4.13。
                  • 不,Node 12 兼容 4.13
                  猜你喜欢
                  • 2022-08-11
                  • 2021-12-30
                  • 2022-08-03
                  • 2016-01-09
                  • 2020-01-05
                  • 2020-09-11
                  • 1970-01-01
                  • 2015-03-31
                  • 2014-01-30
                  相关资源
                  最近更新 更多