旧项目运行遇node-sass/Python问题,npm无法正常安装求解决方案
问题与解决建议
问题详情
- 运行旧项目时遭遇node-sass与Python 2.7相关报错
- 当前无需
--force参数就无法安装任何npm包,已尝试管理员权限操作但未解决 - 询问是否可以回退至之前的npm版本
错误日志
node-sass 安装失败日志
npm ERR! 代码 1 npm ERR! 路径 path\node_modules\node-sass npm ERR! 命令执行失败 npm ERR! 命令: C:\Windows\system32\cmd.exe /d /s /c node scripts/build.js npm ERR! 正在构建: C:\Program Files\nodejs\node.exe path\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library= npm ERR! gyp 提示:执行完成后显示ok即为成功 npm ERR! gyp 详细命令行参数: [ npm ERR! gyp 详细命令行参数 'C:\\Program Files\\nodejs\\node.exe', npm ERR! gyp 详细命令行参数 'C:\\Users\\User\\Desktop\\work\\bw\\bwg-main\\node_modules\\node-gyp\\bin\\node-gyp.js', npm ERR! gyp 详细命令行参数 'rebuild', npm ERR! gyp 详细命令行参数 '--verbose', npm ERR! gyp 详细命令行参数 '--libsass_ext=', npm ERR! gyp 详细命令行参数 '--libsass_cflags=', npm ERR! gyp 详细命令行参数 '--libsass_ldflags=', npm ERR! gyp 详细命令行参数 '--libsass_library=' npm ERR! gyp 详细命令行参数 ] npm ERR! gyp 版本信息:使用 node-gyp@3.8.0 npm ERR! gyp 版本信息:使用 node@16.15.1 | win32 | x64 npm ERR! gyp 详细命令:rebuild [] npm ERR! gyp 详细命令:clean [] npm ERR! gyp 详细操作:删除 "build" 目录 npm ERR! gyp 详细命令:configure [] npm ERR! gyp 详细检查Python:在PATH中查找Python可执行文件 "python2" npm ERR! gyp 详细`which`命令执行失败 错误:未找到 python2 npm ERR! gyp 详细`which`命令执行失败 在 getNotFoundError (path\node_modules\which\which.js:13:12) npm ERR! gyp 详细`which`命令执行失败 在 F (path\node_modules\which\which.js:68:19) npm ERR! gyp 详细`which`命令执行失败 在 E (path\node_modules\which\which.js:80:29) npm ERR! gyp 详细`which`命令执行失败 在 path\node_modules\which\which.js:89:16 npm ERR! gyp 详细`which`命令执行失败 在 path\node_modules\isexe\index.js:42:5 npm ERR! gyp 详细`which`命令执行失败 在 path\node_modules\isexe\windows.js:36:5 npm ERR! gyp 详细`which`命令执行失败 在 FSReqCallback.oncomplete (node:fs:198:21) npm ERR! gyp 详细`which`命令执行失败 python2 错误:未找到 python2 npm ERR! gyp 详细`which`命令执行失败 在 getNotFoundError (path\node_modules\which\which.js:13:12) npm ERR! gyp 详细`which`命令执行失败 在 F (path\node_modules\which\which.js:68:19) npm ERR! gyp 详细`which`命令执行失败 在 E (path\node_modules\which\which.js:80:29) npm ERR! gyp 详细`which`命令执行失败 在 path\node_modules\which\which.js:89:16 npm ERR! gyp 详细`which`命令执行失败 在 path\node_modules\isexe\index.js:42:5 npm ERR! gyp 详细`which`命令执行失败 在 path\node_modules\isexe\windows.js:36:5 npm ERR! gyp 详细`which`命令执行失败 在 FSReqCallback.oncomplete (node:fs:198:21) { npm ERR! gyp 详细`which`命令执行失败 code: 'ENOENT' npm ERR! gyp 详细`which`命令执行失败 } npm ERR! gyp 详细检查Python:在PATH中查找Python可执行文件 "python" npm ERR! gyp 详细`which`命令执行成功 找到 python C:\Python310\python.EXE npm ERR! gyp 配置错误! npm ERR! gyp 错误堆栈:Error: 命令执行失败: C:\Python310\python.EXE -c import sys; print "%s.%s.%s" % sys.version_info[:3]; npm ERR! gyp 错误堆栈 文件 "<string>", 第1行 npm ERR! gyp 错误堆栈 import sys; print "%s.%s.%s" % sys.version_info[:3]; npm ERR! gyp 错误堆栈 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ npm ERR! gyp 错误堆栈 SyntaxError: 调用'print'缺少括号。你是不是想写 print(...)? npm ERR! gyp 错误堆栈 npm ERR! gyp 错误堆栈 在 ChildProcess.exithandler (node:child_process:398:12) npm ERR! gyp 错误堆栈 在 ChildProcess.emit (node:events:527:28) npm ERR! gyp 错误堆栈 在 maybeClose (node:internal/child_process:1092:16) npm ERR! gyp 错误堆栈 在 Process.ChildProcess._handle.onexit (node:internal/child_process:302:5) npm ERR! gyp 错误系统信息:Windows_NT 10.0.19044 npm ERR! gyp 错误命令:"C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\User\\Desktop\\work\\bw\\bwg-main\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" npm ERR! gyp 错误工作目录:path\node_modules\node-sass npm ERR! gyp 错误node版本:v16.15.1 npm ERR! gyp 错误node-gyp版本:v3.8.0 npm ERR! gyp 执行失败! npm ERR! 构建失败,错误代码:1
全局安装n工具失败日志
C:\Users\User>npm install -g n npm ERR! 代码 EBADPLATFORM npm ERR! 不支持该平台:n@9.1.0 需要 {"os":"!win32"} 当前平台:{"os":"win32"} npm ERR! 支持的操作系统:!win32 npm ERR! 当前操作系统:win32
解决方案
1. 回退npm版本(可行)
可以通过指定版本号回退npm,执行以下命令:
- 查看当前npm版本:
npm -v - 回退到适配node16的8.x版本:
npm install -g npm@8.19.4 - 验证版本:
npm -v
2. 解决node-sass与Python版本冲突问题
旧版node-sass依赖Python 2.7,当前系统为Python3,可通过以下方式解决:
- 安装Python 2.7并配置环境变量,确保
python2命令能被识别 - 或通过node-gyp指定Python路径:
npm config set python C:\Python27\python.exe - 推荐替换为dart-sass(无需Python,更稳定):
- 卸载node-sass:
npm uninstall node-sass - 安装dart-sass:
npm install sass - 修改项目中对
sass-loader的配置,将implementation字段从node-sass改为sass
- 卸载node-sass:
3. 解决n工具安装问题
n工具仅支持类Unix系统,Windows系统可使用nvm-windows管理node版本:
- 下载安装nvm-windows后,通过
nvm install <版本号>安装指定node版本,nvm use <版本号>切换版本
内容的提问来源于stack exchange,提问作者Shynghys
相关产品推荐
相关产品推荐

