突然在我的一个jenkins环境构建已经开始失败,而在本地机器,它似乎工作得很好,因为我有python安装,

从日志中,我能够检测到问题是内部依赖关系,即node-gyp v3.5.0从node-sass v3.8.0,当我通过访问这篇关于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

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


当前回答

node-gyp需要旧的Python 2 - link

如果你没有安装它-检查其他关于安装windows-build-tools的答案。

如果您像我一样,同时安装了旧版本和新版本的Python, node-gyp很可能会尝试使用Python 3。这将导致以下SyntaxError:无效语法错误。

我找到了一篇关于安装两个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配置中设置旧的Python版本。你的默认Python仍然是新的。

其他回答

我在Windows 10上的Node 12.19.0和yarn 1.22.5中发现了同样的问题。我通过安装最新的稳定64位python并在python安装期间将路径添加到环境变量来解决这个问题。在安装python之后,我重启了我的机器以获取env vars。

去包装。Json和更改

"node-sass": "4.14.1",

with

"node-sass": "^7.0.1",

它为我工作之后,我可以运行NPM安装

换句话说,您需要使用支持最新python版本的最新包。

注意:Node Saas 7.0.1兼容"react-scripts": "5.0.0"如果你的包。Json有“react-scripts”:“4.0.0”,然后将其更改为

 "react-scripts": "5.0.0", 

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

我解决这个问题的方法是:

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

而且很管用!!

同样的问题,在我的电脑上安装不同版本的python时浪费了几个小时。简单地升级节点到最新版本v8.11.2和npm 5.6.0,然后安装node-sass@4.5.3,你就可以了。

如果节点版本更新,NPM -g将工作。我遇到了这个问题,npm install -g——production windows-build-tools作为临时解决方案。但是从长期来看,升级Node.js、NPM和相关的包版本是一个更好的选择。