Windows11下Git Bash无法识别Node.js,Powershell可正常运行求助
问题:Windows 11 Git Bash中npm安装puppeteer提示'node'未识别
在Windows 11的Git Bash环境下运行npm install --save puppeteer时触发错误,但相同命令在PowerShell或Windows Terminal中可正常执行。已反复确认Node.js路径已添加至系统Path环境变量(尝试过添加Node安装目录路径和node.exe路径)。
错误输出
npm ERR! code 1 npm ERR! path C:\Users\{user}\git-repos\book-scraper\node_modules\puppeteer npm ERR! command failed npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node install.js npm ERR! 'node' is not recognized as an internal or external command, npm ERR! operable program or batch file. npm ERR! A complete log of this run can be found in: npm ERR! C:\Users\{user}\AppData\Local\npm-cache\_logs\2022-08-18T20_26_25_630Z-debug-0.log
日志关键内容
365 verbose pkgid puppeteer@16.1.1 366 verbose cwd C:\Users\{user}\git-repos\book-scraper 367 verbose Windows_NT 10.0.22000 368 verbose node v16.15.0 369 verbose npm v8.17.0 370 error code 1 371 error path C:\Users\{user}\git-repos\book-scraper\node_modules\puppeteer 372 error command failed 373 error command C:\WINDOWS\system32\cmd.exe /d /s /c node install.js 374 error 'node' is not recognized as an internal or external command, 374 error operable program or batch file. 375 verbose exit 1
解决方法
检查Git Bash的Path变量
在Git Bash中执行echo $PATH,查看是否包含Node.js安装路径(如C:\Program Files\nodejs)。若未找到,手动添加:export PATH="$PATH:/c/Program Files/nodejs"注意将路径替换为你的实际Node安装目录,路径中的
\需改为/。让Git Bash继承系统Path
修改Git Bash的~/.bashrc或~/.profile文件,添加以下内容:source /etc/profile保存后重启Git Bash,再执行安装命令。
指定node.exe路径给npm
在Git Bash中执行以下命令,强制npm使用完整的node路径:npm config set node_exec "/c/Program Files/nodejs/node.exe"设置完成后重新运行
npm install --save puppeteer。以管理员身份运行Git Bash
右键Git Bash图标选择「以管理员身份运行」,避免权限问题导致环境变量加载异常。重装Git Bash
若以上方法均无效,可能是Git Bash环境配置损坏,卸载后重新下载最新版本安装。
内容的提问来源于stack exchange,提问作者Ryan Schafer
相关产品推荐
相关产品推荐

