Windows 11下无法单独安装任意npm包的问题求助
问题描述
刚切换到Windows 11系统后,执行npm install安装任何包都会报错(示例为通过ng new创建的全新Angular项目)。执行npm i electron时的报错日志如下:
npm ERR! code ELIFECYCLE npm ERR! syscall spawn bash npm ERR! file bash npm ERR! path bash npm ERR! errno -4058 npm ERR! electron@21.2.1 postinstall: `node install.js` npm ERR! spawn bash ENOENT npm ERR! npm ERR! Failed at the electron@21.2.1 postinstall script. npm ERR! This is probably not a problem with npm. There is likely additional logging output above. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\Timuçin\AppData\Roaming\npm-cache\_logs\2022-11-01T15_25_24_552Z-debug.log PS C:\Users\Timuçin\Documents\Projects\demo-proj>
尝试更换Angular、Node(含LTS版本)及npm版本后问题仍未解决,仅操作系统从Win10更换为Win11,怀疑是Win11导致的问题。
当前环境版本详情:
Angular CLI: 14.2.7 Node: 14.15.3 Package Manager: npm 6.14.9 OS: win32 x64 Angular: 14.2.8 ... animations, common, compiler, compiler-cli, core, forms ... platform-browser, platform-browser-dynamic, router Package Version --------------------------------------------------------- @angular-devkit/architect 0.1402.7 @angular-devkit/build-angular 14.2.7 @angular-devkit/core 14.2.7 @angular-devkit/schematics 14.2.7 @angular/cli 14.2.7 @schematics/angular 14.2.7 rxjs 7.5.7 typescript 4.7.4
原因分析
报错核心是spawn bash ENOENT,说明npm在执行包的postinstall脚本时尝试调用bash shell,但Windows 11默认未预装bash环境;或者npm配置中被设置为默认使用bash作为脚本执行shell,导致系统无法找到对应程序。
解决办法
- 修改npm默认脚本执行shell为Windows原生的cmd或PowerShell:
- 切换为cmd:执行
npm config set script-shell "C:\\Windows\\system32\\cmd.exe" - 切换为PowerShell:执行
npm config set script-shell "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
- 切换为cmd:执行
- 安装Windows Subsystem for Linux(WSL)提供bash环境:
- 以管理员身份打开PowerShell,执行
wsl --install,按提示完成安装后重启系统
- 以管理员身份打开PowerShell,执行
- 检查系统环境变量:确保
%SystemRoot%\system32在PATH环境变量中,避免系统无法找到cmd/PowerShell程序 - 清理npm缓存后重试:执行
npm cache clean --force,再重新运行npm install
内容的提问来源于stack exchange,提问作者Timuçin Çiçek
相关产品推荐
相关产品推荐

