如何调试一个 nodejs addon


修改 building.gyp


conditions 中添加一下编译指令:

"conditions": [
[
"OS=='win'", {
"msvs_settings": {
"VCLinkerTool": {
"VCCLCompilerTool": {
"Optimization": "0",
"DebugInformationFormat": "3"
},
"AdditionalLibraryDirectories": [
...
],
}
}
}
]
]

修改 js 文件


在 js 中加载 *.node 的文件添加断点,以便我们能使用 vs 来 attach 到 electron 进程

const addon = require((0, path_1.join)(__dirname, "..", "build", "debug", "xxx.node"));

这里需要注意:

通常我们的 *.node 文件通常是 build/Release/xxx.node

但是在 debug 环境下,它的目录是 build/debug/xxx.node.

使用 vs 挂载到相应的 electron 进程


运行你的 electron 程序,当js代码中的断点被触发后, 使用 vs 挂载到相应的 electron 进程便可以开始调试了。

Published at:
May 17, 2025
Keywords:
NodeJS
Debug
Windows
c++