执行npm install --legacy-peer-deps失败,node-sass依赖Python问题求助
解决node-sass安装时的Python依赖问题
为解决node-sass的peer dependencies问题,执行npm install --legacy-peer-deps后出现如下报错:
npm ERR! code 1 npm ERR! path /Users/apple/Documents/projects/nova-web/node_modules/node-sass npm ERR! command failed npm ERR! command sh -c node scripts/build.js npm ERR! Building: /usr/local/bin/node /Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/node-gyp/bin/node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library= npm ERR! gyp info it worked if it ends with ok npm ERR! gyp verb cli [ npm ERR! gyp verb cli '/usr/local/bin/node', npm ERR! gyp verb cli '/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/node-gyp/bin/node-gyp.js', npm ERR! gyp verb cli 'rebuild', npm ERR! gyp verb cli '--verbose', npm ERR! gyp verb cli '--libsass_ext=', npm ERR! gyp verb cli '--libsass_cflags=', npm ERR! gyp verb cli '--libsass_ldflags=', npm ERR! gyp verb cli '--libsass_library=' npm ERR! gyp verb cli ] npm ERR! gyp info using node-gyp@3.8.0 npm ERR! gyp info using node@18.15.0 | darwin | x64 npm ERR! gyp verb command rebuild [] npm ERR! gyp verb command clean [] npm ERR! gyp verb clean removing "build" directory npm ERR! gyp verb command configure [] npm ERR! gyp verb check python checking for Python executable "python2" in the PATH npm ERR! gyp verb `which` failed Error: not found: python2 npm ERR! gyp verb `which` failed at getNotFoundError (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:13:12) npm ERR! gyp verb `which` failed at F (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:68:19) npm ERR! gyp verb `which` failed at E (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:80:29) npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:89:16) npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/isexe/index.js:42:5 npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/isexe/mode.js:8:5 npm ERR! gyp verb `which` failed at FSReqCallback.oncomplete (node:fs:208:21) npm ERR! gyp verb `which` failed python2 Error: not found: python2 npm ERR! gyp verb `which` failed at getNotFoundError (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:13:12) npm ERR! gyp verb `which` failed at F (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:68:19) npm ERR! gyp verb `which` failed at E (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:80:29) npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:89:16) npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/isexe/index.js:42:5 npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/isexe/mode.js:8:5 npm ERR! gyp verb `which` failed at FSReqCallback.oncomplete (node:fs:208:21) { npm ERR! gyp verb `which` failed code: 'ENOENT' npm ERR! gyp verb `which` failed } npm ERR! gyp verb check python checking for Python executable "python" in the PATH npm ERR! gyp verb `which` failed Error: not found: python npm ERR! gyp verb `which` failed at getNotFoundError (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:13:12) npm ERR! gyp verb `which` failed at F (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:68:19) npm ERR! gyp verb `which` failed at E (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:80:29) npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:89:16) npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/isexe/index.js:42:5 npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/isexe/mode.js:8:5 npm ERR! gyp verb `which` failed at FSReqCallback.oncomplete (node:fs:208:21) npm ERR! gyp verb `which` failed python Error: not found: python npm ERR! gyp verb `which` failed at getNotFoundError (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:13:12) npm ERR! gyp verb `which` failed at F (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:68:19) npm ERR! gyp verb `which` failed at E (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:80:29) npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:89:16) npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/isexe/index.js:42:5 npm ERR! gyp verb `which` failed at /Users/apple/Documents/projects/nova-web/node_modules/isexe/mode.js:8:5 npm ERR! gyp verb `which` failed at FSReqCallback.oncomplete (node:fs:208:21) { npm ERR! gyp verb `which` failed code: 'ENOENT' npm ERR! gyp verb `which` failed } npm ERR! gyp ERR! configure error npm ERR! gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable. npm ERR! gyp ERR! stack at PythonFinder.failNoPython (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/node-gyp/lib/configure.js:484:19) npm ERR! gyp ERR! stack at PythonFinder.<anonymous> (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/node-gyp/lib/configure.js:406:16) npm ERR! gyp ERR! stack at F (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:68:16) npm ERR! gyp ERR! stack at E (/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:80:29) npm ERR! gyp ERR! stack at /Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/which/which.js:89:16) npm ERR! gyp ERR! stack at /Users/apple/Documents/projects/nova-web/node_modules/isexe/index.js:42:5 npm ERR! gyp ERR! stack at /Users/apple/Documents/projects/nova-web/node_modules/isexe/mode.js:8:5 npm ERR! gyp ERR! stack at FSReqCallback.oncomplete (node:fs:208:21) npm ERR! gyp ERR! System Darwin 21.6.0 npm ERR! gyp ERR! command "/usr/local/bin/node" "/Users/apple/Documents/projects/nova-web/node_modules/node-sass/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" npm ERR! gyp ERR! cwd /Users/apple/Documents/projects/nova-web/node_modules/node-sass npm ERR! gyp ERR! node -v v18.15.0 npm ERR! gyp ERR! node-gyp -v v3.8.0 npm ERR! gyp ERR! not ok npm ERR! Build failed with error code: 1 npm ERR! A complete log of this run can be found in: npm ERR! /Users/apple/.npm/_logs/2023-03-14T13_09_24_326Z-debug-0.log
问题分析与解决方案
报错核心是node-gyp找不到Python可执行文件,你已安装Python3,可以通过以下方式解决:
1. 临时指定Python路径(仅当前终端有效)
在安装前临时设置环境变量:
export PYTHON=/usr/bin/python3 npm install --legacy-peer-deps
2. 永久配置Python环境变量
针对bash环境,编辑~/.bash_profile或~/.bashrc文件,添加:
export PYTHON=/usr/bin/python3
执行以下命令使配置生效:
source ~/.bash_profile
之后再执行安装命令即可。
3. 通过npm config直接指定Python路径
无需修改系统环境变量,直接用npm配置:
npm config set python /usr/bin/python3 npm install --legacy-peer-deps
4. 推荐方案:迁移到dart-sass
node-sass已停止维护,且对高版本Node兼容性差,建议直接替换为sass(dart-sass):
npm uninstall node-sass npm install sass --legacy-peer-deps
此方案可彻底避免node-gyp编译相关问题,稳定性更高。
内容的提问来源于stack exchange,提问作者Drew Gallagher
相关产品推荐
相关产品推荐

