https://blog.csdn.net/adley_app/article/details/95165584
GYP是一种构建自动化工具。
node-gyp:node下的gyp。
为什么要有node-gyp?
npm 为了方便干脆就直接源码分发,用户装的时候再现场编译。
因为node程序中需要调用一些其他语言编写的 工具 甚至是dll,需要先编译一下,否则就会有跨平台的问题,例如在windows上运行的软件copy到mac上就不能用了,但是如果源码支持,编译一下,在mac上还是可以用的。node-gyp在较新的Node版本中都是自带的(平台相关),用来编译原生C++模块。
配置是否正常
win + r cmd 控制台得到如下信息即可,注意安装 MSBuild (2015)工具链。
C:UsersJuwan>node-gyp list
gyp info it worked if it ends with ok
gyp info using node-gyp@6.1.0
gyp info using node@10.13.0 | win32 | ia32
10.13.0
gyp info ok
如何使用
node-gyp rebuild 在 Windows 下可以理解为调用 msbuid 进行该文件夹下的 sln 解决方案的 build ,例如我们在使用 serialport 的时候,经常要对 C++ 的代码进行本机编译得到 DLL 。
如下例子:
PS C:UsersJuwanDocumentsGitHubelectron-vue-admin> cd node_modules@serialportindings
PS C:UsersJuwanDocumentsGitHubelectron-vue-admin
ode_modules@serialportindings> node-gyp rebuild
gyp info it worked if it ends with ok
gyp info using node-gyp@6.1.0
gyp info using node@10.13.0 | win32 | ia32
gyp info find Python using Python version 2.7.17 found at "C:Python27python.exe"
gyp info find VS using VS2019 (16.2.29306.81) found at:
gyp info find VS "C:Program Files (x86)Microsoft Visual Studio2019Enterprise"
gyp info find VS run with --verbose for detailed information
gyp info spawn C:Python27python.exe
gyp info spawn args [ 'C:\Users\Juwan\AppData\Roaming\nvm\v10.13.0\node_modules\node-gyp\gyp\gyp_main.py',
gyp info spawn args 'binding.gyp',
gyp info spawn args '-f',
gyp info spawn args 'msvs',
gyp info spawn args '-I',
gyp info spawn args 'C:\Users\Juwan\Documents\GitHub\electron-vue-admin\node_modules\@serialport\bindings\build\config.gypi',
gyp info spawn args '-I',
gyp info spawn args 'C:\Users\Juwan\AppData\Roaming\nvm\v10.13.0\node_modules\node-gyp\addon.gypi',
gyp info spawn args '-I',
gyp info spawn args 'C:\Users\Juwan\AppData\Local\node-gyp\Cache\10.13.0\include\node\common.gypi',
gyp info spawn args '-Dlibrary=shared_library',
gyp info spawn args '-Dvisibility=default',
gyp info spawn args '-Dnode_root_dir=C:\Users\Juwan\AppData\Local\node-gyp\Cache\10.13.0',
gyp info spawn args '-Dnode_gyp_dir=C:\Users\Juwan\AppData\Roaming\nvm\v10.13.0\node_modules\node-gyp',
gyp info spawn args '-Dnode_lib_file=C:\\Users\\Juwan\\AppData\\Local\\node-gyp\\Cache\\10.13.0\\<(target_arch)\\node.lib',
gyp info spawn args '-Dmodule_root_dir=C:\Users\Juwan\Documents\GitHub\electron-vue-admin\node_modules\@serialport\bindings',
gyp info spawn args '-Dnode_engine=v8',
gyp info spawn args '--depth=.',
gyp info spawn args '--no-parallel',
gyp info spawn args '--generator-output',
gyp info spawn args 'C:\Users\Juwan\Documents\GitHub\electron-vue-admin\node_modules\@serialport\bindings\build',
gyp info spawn args '-Goutput_dir=.' ]
gyp info spawn C:Program Files (x86)Microsoft Visual Studio2019EnterpriseMSBuildCurrentBinMSBuild.exe
gyp info spawn args [ 'build/binding.sln',
gyp info spawn args '/clp:Verbosity=minimal',
gyp info spawn args '/nologo',
gyp info spawn args '/p:Configuration=Release;Platform=Win32' ]
在此解决方案中一次生成一个项目。若要启用并行生成,请添加“-m”开关。
serialport.cpp
serialport_win.cpp
win_delay_load_hook.cc
正在创建库 C:UsersJuwanDocumentsGitHubelectron-vue-admin
ode_modules@serialportindingsuildReleaseindings.lib 和对象 C:UsersJuwanDocumentsGitHubelectron-vue-admin
ode_
modules@serialportindingsuildReleaseindings.exp
正在生成代码
Previous IPDB not found, fall back to full compilation.
All 478 functions were compiled because no usable IPDB/IOBJ from previous compilation was found.
已完成代码的生成
bindings.vcxproj -> C:UsersJuwanDocumentsGitHubelectron-vue-admin
ode_modules@serialportindingsuildRelease\bindings.node
gyp info ok
PS C:UsersJuwanDocumentsGitHubelectron-vue-admin
ode_modules@serialportindings>
常见错误
关联 Error: A dynamic link library (DLL) initialization routine failed.