用 vscode調試nodejs 時,提示對應的 grpc 模塊找不到

報錯提示以下:node

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.pnggit

運行時的node版本是 11.10.1 版本,對應的 modules 版本是 67,因此當前版本下,和grpc版本對應的模塊就是:node-v67-darwin-x64-unknown。
但我本地實際的版本是 14.13.0 版本,對應的 grpc 模塊是 node-v83-darwin-x64-unknown,因此報錯找不到:
image.pnggithub

究其緣由,是vscode自己是一個js應用,使用的 electron 框架開發的桌面應用,裏邊內置的nodejs版本當前是 11.10.1 版本,且沒法被改變:
image.pngnpm

解決辦法,是在vscode的調試配置 .vscode/launch.json 文件裏,用配置項runtimeVersion配置指定的node版:json

{
    "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:bootstrap

相關文章
相關標籤/搜索