乐趣区

关于node.js:用-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 运行时的版本不对:

运行时的 node 版本是 11.10.1 版本,对应的 modules 版本是 67,所以以后版本下,和 grpc 版本对应的模块就是:node-v67-darwin-x64-unknown。
但我本地理论的版本是 14.13.0 版本,对应的 grpc 模块是 node-v83-darwin-x64-unknown,所以报错找不到:

究其原因,是 vscode 自身是一个 js 利用,应用的 electron 框架开发的桌面利用,里边内置的 nodejs 版本以后是 11.10.1 版本,且无奈被扭转:

解决办法,是在 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:

  • https://github.com/microsoft/vscode/issues/18253
  • https://stackoverflow.com/questions/45062881/custom-node-version-to-run-vscode-extensions
退出移动版