突然在我的一个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

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


当前回答

最近我在Windows上遇到了这样的情况。我通过以下步骤修复它使用一个具有管理权限的PowerShell:

删除node_modulesfolder 使用管理权限运行NPM install——global windows-build-tools。(在我的情况下需要重启-和重启没有问!!) 使用NPM install重新安装节点模块或node-sass

其他回答

node-sass已于2020年10月26日弃用

不需要安装旧版本的Python,只需迁移到dart.sass即可

对于我的ReactJS项目(npm),以下步骤对我有效:

从项目中删除node_modules 在包中。Json,删除这个 依赖: “依赖”:{ …, :“node-sass ^ 3.0.0”, ... } NPM install——save-dev sass npm安装 现在尝试使用npm start运行你的项目 如果你收到一个类似于"To import Sass files, you first need To install node-sass"的错误,执行如下命令: NPM安装node-sass@npm:sass

如果你正在使用yarn,执行相同的命令,但在最后一步执行这个命令,如下所述: 纱线拆卸节点 纱加纱


看了所有的答案后,我发现这个解决方案可能非常有用。它解释了在安装node-sass时如何配置“npm”来找到你安装的python版本。记住,node-sass需要node-gyp(一个npm构建工具)来查找你的python路径。或者只是安装python,并遵循相同的解决方案(在链接中)。谢谢。

我在一台Windows 11机器上,为了修复它,我自己下载了python27,使用这个链接:https://www.python.org/download/releases/2.7/。

安装完成后,我运行了这个命令:

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

我不得不:

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.js LTS版本:18.13.0的Windows 11上得到这个错误(包括npm 8.19.3)

https://nodejs.org/en/download/

修复了第一次手动下载Python 2.7.18,因为只安装了Python 3.11。

https://www.python.org/downloads/windows/

https://www.python.org/downloads/release/python-2718/

安装后,我从@bvdb运行命令:

npm config set python "C:\Python27\python.exe"

来源:

https://stackoverflow.com/a/64486670/3850405

这修复了错误