用 vscode调试nodejs 时,提示对应的 grpc 模块找不到

默凡

报错提示如下:

ERROR 2020-12-07 15:31:14,835 v1(6) [gulu-core]::UncaughtException 10.79.185.0 - - default - Error: Failed to load gRPC binary module because it was not installed for the current system
Expected directory: node-v67-darwin-x64-unknown
Found: [node-v83-darwin-x64-unknown]
This problem can often be fixed by running "npm rebuild" on the current system
Original error: Cannot find module '/Users/chenwei/repos/kunpeng/node_modules/grpc/src/node/extension_binary/node-v67-darwin-x64-unknown/grpc_node.node'
    at Object.<anonymous> (/Users/chenwei/repos/kunpeng/node_modules/grpc/src/grpc_extension.js:53:17)
    at Module._compile (internal/modules/cjs/loader.js:738:30)
    at Module._extensions..js (internal/modules/cjs/loader.js:749:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/chenwei/repos/kunpeng/node_modules/ts-node/src/index.ts:851:44)
    at Module.load (internal/modules/cjs/loader.js:630:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:570:12)
    at Function.Module._load (internal/modules/cjs/loader.js:562:3)
    at Module.require (internal/modules/cjs/loader.js:667:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at Object.<anonymous> (/Users/chenwei/repos/kunpeng/node_modules/grpc/src/client_interceptors.js:144:12)
Waiting for the debugger to disconnect...

调试了一下grpc模块,发现是调试模式下,node运行时的版本不对:
image.png

运行时的node版本是 11.10.1 版本,对应的 modules 版本是 67,所以当前版本下,和grpc版本对应的模块就是:node-v67-darwin-x64-unknown。
但我本地实际的版本是 14.13.0 版本,对应的 grpc 模块是 node-v83-darwin-x64-unknown,所以报错找不到:
image.png

究其原因,是vscode本身是一个js应用,使用的 electron 框架开发的桌面应用,里边内置的nodejs版本当前是 11.10.1 版本,且无法被改变:
image.png

解决办法,是在vscode的调试配置 .vscode/launch.json 文件里,用配置项runtimeVersion配置指定的node版:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "runtimeVersion": "14.13.0",
            "request": "launch",
            "name": "native_server",
            "console": "integratedTerminal",
            "cwd": "${workspaceFolder}/projects/native_server",
            "runtimeArgs": ["-r", "ts-node/register/transpile-only"],
            "args": ["./server/bootstrap.ts"]
        }
    ]
}

参考讨论issue:

阅读 1.1k

知行合一,天道酬勤

1.2k 声望
236 粉丝
0 条评论
你知道吗?

知行合一,天道酬勤

1.2k 声望
236 粉丝
宣传栏