报错案例

在安装webpack的webpack-dev-server的时候,会爆gypERR的错误,当时因为是在自己的项目的局部安装(本地安装)
所以,我通过在项目的package.json的devDependencies中增加"webpack-dev-server": "^4.0.0"
再在命令行执行:npm install,从而解决了安装不了的问题。
但是最近在全局安装vue-cli的时候,又爆这个错误!下面是这个错误的样子,这个错误可能让你安装python
或者让你安装Visual Studio,下面是这个报错的样子:

D:\webStorm\Project\vue_test>npm install @vue/cli -g
...不重要的省略
npm ERR! code 1
npm ERR! path D:\nodeJs\node_global\node_modules\@vue\cli\node_modules\bufferutil
npm ERR! command failed
npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@7.1.2
npm ERR! gyp info using node@12.13.1 | win32 | x64
//是的,我屈服了,我安装了python,结果安装完又报错,让我安装Visual Studio
npm ERR! gyp info find Python using Python version 3.9.6 found at "D:\python\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version was set from command line or npm config
//你可能需要设置一下:npm config set msvs_version 2019,不然找不到你安装的VS,是的,你可能也要安装下VS2019版本
npm ERR! gyp ERR! find VS - looking for Visual Studio version 2019	
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS valid versions for msvs_version:
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm ERR! gyp ERR! stack     at VisualStudioFinder.fail (D:\nodeJs\node_global\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:121:47)
npm ERR! gyp ERR! stack     at D:\nodeJs\node_global\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:74:16
npm ERR! gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (D:\nodeJs\node_global\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:351:14)
npm ERR! gyp ERR! stack     at D:\nodeJs\node_global\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:70:14
npm ERR! gyp ERR! stack     at D:\nodeJs\node_global\node_modules\npm\node_modules\node-gyp\lib\find-visualstudio.js:372:16
npm ERR! gyp ERR! stack     at D:\nodeJs\node_global\node_modules\npm\node_modules\node-gyp\lib\util.js:54:7
npm ERR! gyp ERR! stack     at D:\nodeJs\node_global\node_modules\npm\node_modules\node-gyp\lib\util.js:33:16
npm ERR! gyp ERR! stack     at ChildProcess.exithandler (child_process.js:302:5)
npm ERR! gyp ERR! stack     at ChildProcess.emit (events.js:210:5)
npm ERR! gyp ERR! stack     at maybeClose (internal/child_process.js:1021:16)
npm ERR! gyp ERR! System Windows_NT 10.0.19042
npm ERR! gyp ERR! command "D:\\nodeJs\\node.exe" "D:\\nodeJs\\node_global\\node_modules\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd D:\nodeJs\node_global\node_modules\@vue\cli\node_modules\bufferutil
npm ERR! gyp ERR! node -v v12.13.1
npm ERR! gyp ERR! node-gyp -v v7.1.2
npm ERR! gyp ERR! not ok

具体解决办法:

  • 如果你安装的依赖是安装在项目中,即局部安装(本地安装),你可以通过修改项目的package.json来安装

  • 如果你安装的依赖是全局安装,没有办法,bug是爸爸

    • 先设置一下msvs_version

      //查看当前的msvs_version
      npm config get msvs_version
      //修改成你安装的VS版本
      npm config set msvs_version 2019
    • 然后安装python和Visual Studio,安装VS的时候你可能需要勾选Desktop development with C++
      我也不知道勾选有没有用,反正不放过任何一处,安装完毕后,重新下载依赖就能成功了。

2023-6-8更新

今天又碰到这问题了,对这个问题理解得更深一点了,因为很多前端项目使用的node_gyp版本太低
它会去找低版本的python,例如python2,而我们现在电脑一般装的是python3,所以报错
这个node_gyp不光要python支持,还需要C++的支持,还有msvs依赖吧,如果遇到了python报错,
但你自己又有python依赖的时候,大概率是它需要的依赖版本和你的版本不同

npm config set pytyon python  
这是指当node_gyp编译需要python时,去找系统中python程序,这个python不是python的版本
你应该看报错,报错中会有显示node_gyp找到你系统中的python但是这个版本太高,它用不了,
因为gyp版本决定只能使用低版本的python,所以这句命令让gyp去指定用你系统安装的python去编译它的代码

报错可以看到,gyp它找python的过程
gyp verb check python checking for Python executable "python2" in the PATH
gyp verb `which` failed Error: not found: python2

gyp verb `which` failed  python2 Error: not found: python2

gyp verb check python checking for Python executable "python" in the PATH
gyp verb `which` succeeded python D:\python\python.EXE

所以就用这个"python",如果你的这里是python3,那命名最后的python就要改为python3

关于此问题解决文章