buildcheck包报错问题排查解决

2023-02-09 11:54:21
目录
问题原因解决方法一方法二方法三方法四总结

问题

最近项目开发中遇到>

因项目本地部署需要上传打包文件,使用到了 ssh2-sftp-client 包,在拉取包的时候控制台抛如下错误:

warning Error running install script for optional dependency: "D:\\Projects\\xxx\\node_modules\\cpu-features: Command failed.
Exit code: 1
Command: node buildcheck.js > buildcheck.gypi && node-gyp rebuild
Arguments:
Directory: D:\\Projects\\xxx\\node_modules\\cpu-features
Output:
D:\\Projects\\xxx\\node_modules\\buildcheck\\lib\\index.js:133
        throw new Error('Unable to detect compiler type');
        ^
Error: Unable to detect compiler type
    at new BuildEnvironment (D:\\Projects\\vite-ui\\packages\\HtUpssh\\node_modules\\buildcheck\\lib\\index.js:133:15)
    at Object.<anonymous> (D:\\Projects\\vite-ui\\packages\\HtUpssh\\node_modules\\cpu-features\\buildcheck.js:5:12)
    at Module._compile (internal/modules/cjs/loader.js:1085:14)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47"

该错误不会影响文件上传功能,但是有问题还是要想办法解决掉。

原因

经过排查发现,该报错是因为:

    ssh2-sftp-client>ssh2 包依赖了可选包 cpu-features 包。

      cpu-features 包依赖了 buildcheck 包;并且在包安装的时候执行了 install 命令,运行了 buildcheck.js 脚本来检查node的构建环境;而 buildcheck.js 里引用了 buildcheck 包,执行其源码。

      buildcheck.js 代码:

        buildcheck 包里代码在执行时,匹配不到系统可支持的工具类型,就抛错了。

        通过 buildcheck 源码的排查,当是 windows 系统时,会去查找系统安装的工具包,匹配其版本号,获取其类型,根据不同的类型走不同的编译逻辑。(buildcheck源码没有细看,粗略看了下,理解错误的话麻烦指正)

        我电脑上查找出的 packages 里 Microsoft.VisualStudio.Component.Windows10SDK 没有版本号,所以匹配失败,导致检测不出编译类型,从而抛错。

        解决

        以上报错是在>

          Windows10SDK 补充版本号buildcheck 包源码仅下载不执行移除对 cpu-features 包的依赖

          因为 buildcheck 的源码执行是在 install 的时候,所以通过打补丁的方法也不能解决,补丁是在 install 之后。

          方法一

          Windows10SDK>

          方法二

          buildcheck>

          yarn add -D buildcheck --ignore-scripts
          

          或者 .yarnrc 文件中配置:

          ignore-scripts 'true'
          

          或者 .npmrc 文件中配置:

          ignore-scripts = true
          

          添加 ignore-scripts 后,npm scripts 命令将不会执行。那 buildcheck 源码逻辑也就不会执行了。但是这个命令会导致 npm 其他的命令也不执行,会有一定风险。该方法也不建议。

          方法三

          移除对>

          可以在 packages-lock.json 去掉对 cpu-features 包的依赖。

          或者在 yarn.lock 去掉对 cpu-features 包的依赖。

          但是锁包的文件容易被篡改,而且增加维护成本。该方法也不是特别适合。

          方法四

          重发>

          项目中也是采用这个方法来解决。如果大家有更好的办法,欢迎留言。

          总结

          遇到问题不能逃避,抽丝剥茧,总能解决的,无非是时间问题罢了。

          以上就是buildcheck包报错问题排查解决的详细内容,更多关于buildcheck包报错排查的资料请关注易采站长站其它相关文章!