如何在 Deepin 操作系统上调试 nodejs addon


编译 Debug 版本的 Node.js Addon


要构建 Debug 版本,需要添加 -debug 标志

node-gyp rebuild --target=31.7.7 --dist-url=https://electronjs.org/headers -debug

注意,这里使用 "-Debug", 部分 AI 给的方案是使用 "--Debug", 是不对的。

修改 js 文件


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

我这里的 js 文件是位于项目目录的 dist/index.js 文件。

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

这里需要注意:

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

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

为了方便的获取 electron 项目加载 nodejs addon 的进程 id, 可以使用

console.log(process.pid);

命令,很是方便好用。

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


需要在对应的 addon 项目中,创建 .vscode/launch.json 文件,内容如下:

{
"version": "0.2.0",
"configurations": [
{
"name": "[Linux]Attach to Electron",
"type": "cppdbg",
"request": "attach",
"program": "/xx/node_modules/electron/dist/electron",
"processId": "${command:pickProcess}",
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"visualizerFile": "${workspaceFolder}/.vscode/gdb-visualizers.py"
}
]
}

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

分类

Published at:
October 16, 2025
Keywords:
NodeJS
Debug
Deepin
Linux
c++