Node.js:由于node-sass和node-gyp

时间:2017-08-21 16:02:38

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

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

我从日志中发现,当我研究时,问题是来自 node-sass v3.8.0 node-gyp v3.5.0 的内部依赖性问题通过访问有关node-gyp的this,找到了需要安装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

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

20 个答案:

答案 0 :(得分:32)

所以最近我在Windows上发生了这种情况。我通过使用具有管理员权限的PowerShell执行以下步骤来解决此问题:

  1. 删除npm install --global windows-build-tools文件夹
  2. 运行npm install
  3. 使用sprintf()重新安装节点模块或node-sass

答案 1 :(得分:7)

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

答案 2 :(得分:5)

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

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

我最终要做的是:

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

答案 3 :(得分:3)

今年2岁,但没有一个帮助我。

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

答案 4 :(得分:3)

该错误消息表示无法找到您的python可执行文件或二进制文件。

在许多情况下,它安装在c:\ python27。 如果尚未安装,则可以使用npm install --global windows-build-tools进行安装,只有在尚未安装的情况下,它才能工作。

将其添加到环境变量并不总是有效。 更好的替代方法是在npm配置中进行设置。

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

答案 5 :(得分:2)

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

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

答案 6 :(得分:2)

node-gyp 需要旧的 Python 2 -link

如果尚未安装,请检查有关安装 windows-build-tools 的其他答案。

如果您和我一样,并且同时安装了旧的和新的Python版本,则node-gyp可能会尝试使用Python3。这将导致以下SyntaxError: invalid syntax错误。

我找到了一篇有关安装两个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仍将是新的。

答案 7 :(得分:1)

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

答案 8 :(得分:1)

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

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

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

另一种可能的解决方案:

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

中删除 node-sass 元素

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

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

答案 9 :(得分:1)

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 :(得分:0)

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

答案 11 :(得分:0)

嘿,请按照以下步骤解决此错误

  • 首先我卸载了 python 3.8.6 (最新版本)
  • 然后我安装了 python 2.7.1 (任何 Python 2 版本都可以使用,但是版本不老,建议这样做)
  • 然后我在环境变量中添加了c:\python27
  • 我的操作系统是 windows ,所以我遵循了link
  • 有效

答案 12 :(得分:0)

我的机器是 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

它有效!!

答案 13 :(得分:0)

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

答案 14 :(得分:0)

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

答案 15 :(得分:0)

以下是在运行基于旧 Error: not found: python2 的项目时帮助我防止错误 node-gyp v3.8.0 的方法。

  1. 运行npm install --global --production windows-build-tools@4.0.0
  2. python.exe 中的 python2.exe 重命名为 %USERPROFILE%\.windows-build-tools\python27
  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。

答案 16 :(得分:0)

你也可以尝试使用 sass 模块代替 node-sass:

npm install sass

答案 17 :(得分:0)

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

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

答案 18 :(得分:0)

我必须:

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版本引起的。

答案 19 :(得分:0)

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

enter image description here

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