重装Python后执行npm install仍报错的技术求助
node-sass 安装失败问题解决指南
问题概述
执行npm install时先后遇到两个问题:
- 初始使用高版本Python(3.11)时,旧版node-gyp(v3.8.0)因Python语法不兼容报错
- 切换到Python 2.7后,又出现MSBuild路径找不到的错误
第一次报错信息
6540 verbose npm v8.1.2 6541 error code 1 6542 error path C:\wamp\www\xxxxx\node_modules\node-sass 6543 error command failed 6544 error command C:\Windows\system32\cmd.exe /d /s /c node scripts/build.js 6545 error Building: C:\Program Files\nodejs\node.exe C:\wamp\www\xxxxx\node_modules\node-gyp\bin\node-gyp.js rebuild --verbose --libsass_ext= --libsass_cflags= --libsass_ldflags= --libsass_library= 6546 error gyp info it worked if it ends with ok 6546 error gyp verb cli [ 6546 error gyp verb cli 'C:\\Program Files\\nodejs\\node.exe', 6546 error gyp verb cli 'C:\\wamp\\www\\xxxxx\\node_modules\\node-gyp\\bin\\node-gyp.js', 6546 error gyp verb cli 'rebuild', 6546 error gyp verb cli '--verbose', 6546 error gyp verb cli '--libsass_ext=', 6546 error gyp verb cli '--libsass_cflags=', 6546 error gyp verb cli '--libsass_ldflags=', 6546 error gyp verb cli '--libsass_library=' 6546 error gyp verb cli ] 6546 error gyp info using node-gyp@3.8.0 6546 error gyp info using node@16.13.1 | win32 | x64 6546 error gyp verb command rebuild [] 6546 error gyp verb command clean [] 6546 error gyp verb clean removing "build" directory 6546 error gyp verb command configure [] 6546 error gyp verb check python checking for Python executable "C:\Python311\python.exe" in the PATH 6546 error gyp verb `which` succeeded C:\Python311\python.exe C:\Python311\python.exe 6546 error gyp ERR! configure error 6546 error gyp ERR! stack Error: Command failed: C:\Python311\python.exe -c import sys; print "%s.%s.%s" % sys.version_info[:3]; 6546 error gyp ERR! stack File "<string>", line 1 6546 error gyp ERR! stack import sys; print "%s.%s.%s" % sys.version_info[:3]; 6546 error gyp ERR! stack ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 6546 error gyp ERR! stack SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? 6546 error gyp ERR! stack 6546 error gyp ERR! stack at ChildProcess.exithandler (node:child_process:397:12) 6546 error gyp ERR! stack at ChildProcess.emit (node:events:390:28) 6546 error gyp ERR! stack at maybeClose (node:internal/child_process:1064:16) 6546 error gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) 6546 error gyp ERR! System Windows_NT 10.0.19045 6546 error gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\wamp\\www\\xxxxx\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" 6546 error gyp ERR! cwd C:\wamp\www\xxxxx\node_modules\node-sass 6546 error gyp ERR! node -v v16.13.1 6546 error gyp ERR! node-gyp -v v3.8.0 6546 error gyp ERR! not ok 6546 error Build failed with error code: 1 6547 verbose exit 1
切换Python 2.7后的报错信息
npm ERR! code 1 npm ERR! path C:\wamp\www\xxxxx\node_modules\node-sass npm ERR! command failed npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node scripts/build.js npm ERR! Building: C:\Program Files\nodejs\node.exe C:\wamp\www\xxxxx\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 'C:\\Program Files\\nodejs\\node.exe', npm ERR! gyp verb cli 'C:\\wamp\\www\\xxxxx\\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@16.13.1 | win32 | 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 "C:\Python27\python.exe" in the PATH npm ERR! gyp verb `which` succeeded C:\Python27\python.exe C:\Python27\python.exe npm ERR! gyp verb check python version `C:\Python27\python.exe -c "import sys; print "2.7.2 npm ERR! gyp verb check python version .%s.%s" % sys.version_info[:3];"` returned: %j npm ERR! gyp verb get node dir no --target version specified, falling back to host node version: 16.13.1 npm ERR! gyp verb command install [ '16.13.1' ] npm ERR! gyp verb install input version string "16.13.1" npm ERR! gyp verb install installing version: 16.13.1 npm ERR! gyp verb install --ensure was passed, so won't reinstall if already installed npm ERR! gyp verb install version is already installed, need to check "installVersion" npm ERR! gyp verb got "installVersion" 9 npm ERR! gyp verb needs "installVersion" 9 npm ERR! gyp verb install version is good npm ERR! gyp verb get node dir target node version installed: 16.13.1 npm ERR! gyp verb build dir attempting to create "build" dir: C:\wamp\www\xxxxx\node_modules\node-sass\build npm ERR! gyp verb build dir "build" dir needed to be created? C:\wamp\www\xxxxx\node_modules\node-sass\build npm ERR! gyp verb find vs2017 Found installation at: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community npm ERR! gyp verb find vs2017 - Found Microsoft.VisualStudio.Component.Windows10SDK.19041 npm ERR! gyp verb find vs2017 - Found Microsoft.VisualStudio.Component.VC.Tools.x86.x64 npm ERR! gyp verb find vs2017 - Found Microsoft.VisualStudio.VC.MSBuild.Base npm ERR! gyp verb find vs2017 - Using this installation with Windows 10 SDK npm ERR! gyp verb find vs2017 using installation: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community npm ERR! gyp verb build/config.gypi creating config file npm ERR! gyp verb build/config.gypi writing out config file: C:\wamp\www\xxxxx\node_modules\node-sass\build\config.gypi npm ERR! (node:15060) [DEP0150] DeprecationWarning: Setting process.config is deprecated. In the future the property will be read-only. npm ERR! (Use `node --trace-deprecation ...` to show where the warning was created) npm ERR! gyp verb config.gypi checking for gypi file: C:\wamp\www\xxxxx\node_modules\node-sass\config.gypi npm ERR! gyp verb common.gypi checking for gypi file: C:\wamp\www\xxxxx\node_modules\node-sass\common.gypi npm ERR! gyp verb gyp gyp format was not specified; forcing "msvs" npm ERR! gyp info spawn C:\Python27\python.exe npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args 'C:\\wamp\\www\\xxxxx\\node_modules\\node-gyp\\gyp\\gyp_main.py', npm ERR! gyp info spawn args 'binding.gyp', npm ERR! gyp info spawn args '-f', npm ERR! gyp info spawn args 'msvs', npm ERR! gyp info spawn args '-G', npm ERR! gyp info spawn args 'msvs_version=2015', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args 'C:\\wamp\\www\\xxxxx\\node_modules\\node-sass\\build\\config.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args 'C:\\wamp\\www\\xxxxx\\node_modules\\node-gyp\\addon.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args 'C:\\Users\\nikla\\.node-gyp\\16.13.1\\include\\node\\common.gypi', npm ERR! gyp info spawn args '-Dlibrary=shared_library', npm ERR! gyp info spawn args '-Dvisibility=default', npm ERR! gyp info spawn args '-Dnode_root_dir=C:\\Users\\nikla\\.node-gyp\\16.13.1', npm ERR! gyp info spawn args '-Dnode_gyp_dir=C:\\wamp\\www\\xxxxx\\node_modules\\node-gyp', npm ERR! gyp info spawn args '-Dnode_lib_file=C:\\Users\\nikla\\.node-gyp\\16.13.1\\<(target_arch)\\node.lib', npm ERR! gyp info spawn args '-Dmodule_root_dir=C:\\wamp\\www\\xxxxx\\node_modules\\node-sass', npm ERR! gyp info spawn args '-Dnode_engine=v8', npm ERR! gyp info spawn args '--depth=.', npm ERR! gyp info spawn args '--no-parallel', npm ERR! gyp info spawn args '--generator-output', npm ERR! gyp info spawn args 'C:\\wamp\\www\\xxxxx\\node_modules\\node-sass\\build', npm ERR! gyp info spawn args '-Goutput_dir=.' npm ERR! gyp info spawn args ] npm ERR! gyp verb command build [] npm ERR! gyp verb build type Release npm ERR! gyp verb architecture x64 npm ERR! gyp verb node dev dir C:\Users\nikla\.node-gyp\16.13.1 npm ERR! gyp verb found first Solution file build/binding.sln npm ERR! gyp verb using MSBuild: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\15.0\Bin\MSBuild.exe npm ERR! gyp info spawn C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\15.0\Bin\MSBuild.exe npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args 'build/binding.sln', npm ERR! gyp info spawn args '/nologo', npm ERR! gyp info spawn args '/p:Configuration=Release;Platform=x64' npm ERR! gyp info spawn args ] npm ERR! gyp ERR! UNCAUGHT EXCEPTION npm ERR! gyp ERR! stack Error: spawn C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\15.0\Bin\MSBuild.exe ENOENT npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:282:19) npm ERR! gyp ERR! stack at onErrorNT (node:internal/child_process:477:16) npm ERR! gyp ERR! stack at processTicksAndRejections (node:internal/process/task_queues:83:21) npm ERR! gyp ERR! System Windows_NT 10.0.19045 npm ERR! gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\wamp\\www\\xxxxx\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library=" npm ERR! gyp ERR! cwd C:\wamp\www\xxxxx\node_modules\node-sass npm ERR! gyp ERR! node -v v16.13.1 npm ERR! gyp ERR! node-gyp -v v3.8.0 npm ERR! gyp ERR! This is a bug in `node-gyp`. npm ERR! gyp ERR! Try to update node-gyp and file an Issue if it does not help: npm ERR! gyp ERR! <https://github.com/nodejs/node-gyp/issues> npm ERR! Build failed with error code: 7
解决步骤
1. 锁定Python 2.7版本
旧版node-gyp(v3.8.0)仅支持Python 2.7,需明确指定npm使用Python 2.7:
npm config set python C:\Python27\python.exe
2. 修复MSBuild路径问题
报错显示VS2019的MSBuild路径被错误识别为15.0(实际VS2019对应16.0或Current),手动指定正确路径:
npm config set msbuild_path "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\MSBuild\Current\Bin\MSBuild.exe"
或者更新node-gyp到支持VS2019的新版本:
npm install -g node-gyp@latest
3. 清理缓存后重新安装
- 删除项目目录下的
node_modules文件夹和package-lock.json文件 - 清理npm缓存:
npm cache clean --force - 重新执行安装:
npm install
4. 长期解决方案:替换为dart-sass
node-sass已停止维护,建议迁移到官方推荐的dart-sass:
npm uninstall node-sass npm install sass
项目中原有引用node-sass的代码无需大幅修改,dart-sass兼容大部分API。
内容的提问来源于stack exchange,提问作者Niklas
相关产品推荐
相关产品推荐

