记一次 node-gyp 导致的 npm 包安装报错问题


2021-11-18 node

# 环境

  • macOS 12.0.1

  • node v16.13.0

  • node-gyp v3.8.0

# 背景知识

为什么要有node-gyp,是由于node程序中需要调用一些其他语言编写的工具甚至是dll,需要先编译一下,否则就会有跨平台的问题,例如在windows上运行的软件copy到mac上就不能用了,但是如果源码支持,编译一下,在mac上还是可以用的。node-gyp在较新的Node版本中都是自带的(平台相关),用来编译原生C++模块。

# 问题复现

执行完 npm install 后,报错核心信息如下:

npm ERR! In file included from ../src/binding.cpp:1:
npm ERR! In file included from ../../nan/nan.h:58:
npm ERR! In file included from /Users/anrunlu/.node-gyp/16.13.0/include/node/node.h:63:
npm ERR! In file included from /Users/anrunlu/.node-gyp/16.13.0/include/node/v8.h:30:
npm ERR! /Users/anrunlu/.node-gyp/16.13.0/include/node/v8-internal.h:492:38: error: no template named 'remove_cv_t' in namespace 'std'; did you mean 'remove_cv'?
npm ERR!             !std::is_same<Data, std::remove_cv_t<T>>::value>::Perform(data);
npm ERR!                                 ~~~~~^~~~~~~~~~~
npm ERR!                                      remove_cv
npm ERR! /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/c++/v1/type_traits:710:50: note: 'remove_cv' declared here
npm ERR! template <class _Tp> struct _LIBCPP_TEMPLATE_VIS remove_cv
npm ERR!                                                  ^
npm ERR! 1 error generated.
npm ERR! make: *** [Release/obj.target/binding/src/binding.o] Error 1
npm ERR! gyp ERR! build error 
npm ERR! gyp ERR! stack Error: `make` failed with exit code: 2
npm ERR! gyp ERR! stack     at ChildProcess.onExit (/Users/anrunlu/Dev/nodeProjects/graph-teach-frontend/node_modules/node-gyp/lib/build.js:262:23)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:390:28)
npm ERR! gyp ERR! stack     at Process.ChildProcess._handle.onexit (node:internal/child_process:290:12)
npm ERR! gyp ERR! System Darwin 21.1.0
npm ERR! gyp ERR! command "/usr/local/bin/node" "/Users/anrunlu/Dev/nodeProjects/graph-teach-frontend/node_modules/node-gyp/bin/node-gyp.js" "rebuild" "--verbose" "--libsass_ext=" "--libsass_cflags=" "--libsass_ldflags=" "--libsass_library="
npm ERR! gyp ERR! cwd /Users/anrunlu/Dev/nodeProjects/graph-teach-frontend/node_modules/node-sass
npm ERR! gyp ERR! node -v v16.13.0
npm ERR! gyp ERR! node-gyp -v v3.8.0
npm ERR! gyp ERR! not ok 
npm ERR! Build failed with error code: 1

# 问题解决

可以看到,报错原因是一处 c++ 变量未找到,根据报错提示:

npm ERR! /Users/anrunlu/.node-gyp/16.13.0/include/node/v8-internal.h:492:38: error: no template named 'remove_cv_t' in namespace 'std'; did you mean 'remove_cv'?
npm ERR!             !std::is_same<Data, std::remove_cv_t<T>>::value>::Perform(data);
npm ERR!                                 ~~~~~^~~~~~~~~~~
npm ERR!                                      remove_cv

去往 /Users/anrunlu/.node-gyp/16.13.0/include/node/v8-internal.h:492:38这个坐标,将 remove_cv_t更改为remove_cv

保存之后再次执行安装,无报错,所有模块安装成功。

# 还没结束

这个项目是一个 vue 前端项目,在所有包安装成功后,执行 npm run dev 准备开发调试时,又遇到错误:

Vue packages version mismatch:

- vue@2.6.14 (/Users/anrunlu/Dev/nodeProjects/graph-teach-frontend/node_modules/vue/dist/vue.runtime.common.js)
- vue-template-compiler@2.6.11 (/Users/anrunlu/Dev/nodeProjects/graph-teach-frontend/node_modules/vue-template-compiler/package.json)

This may cause things to work incorrectly. Make sure to use the same version for both.
If you are using vue-loader@>=10.0, simply update vue-template-compiler.
If you are using vue-loader@<10.0 or vueify, re-installing vue-loader/vueify should bump vue-template-compiler to the latest.

这是一个 vue 与 vue-template-compiler 版本不匹配的错误,然后我执行 npm install vue@2.6.11 --save把 vue 降低到与 vue-template-compiler 一致的版本。安装完毕后,再次启动,终于成功。

# 总结

  • 不要被一大堆红色的 err 给吓倒,仔细去寻找报错的根源
  • 敢于去尝试修改底层的代码

这是我第一次在 Apple Silicon M1 芯片平台上进行的 node 项目开发。因为想让 M1 发挥它的性能,所以安装了最新版本的 nodejs。在调试成功这个项目之前遇到了不少让我头大的兼容性问题,也曾一度想要放弃使用最新版 node 而换用运行在转译下的稳定版,但是又仔细想想,世上无难事,只怕有心人,没有什么解决不了的 bug。既要有敢为人先的精神,也要有敢为人先的勇气,既然选择了最新版,那就继续冲吧。

Last Updated: 11/18/2021, 8:40:53 AM