【问题标题】:use grpc in nodejs and typescript在 nodejs 和 typescript 中使用 grpc
【发布时间】:2021-12-29 10:08:06
【问题描述】:

我需要在 nodejs 一个类型脚本项目中使用 GRPC,我在 windows 10 和 npm 中生成 proto 文件时遇到问题。

我想读这个文件transpile-proto-ts.sh:

#!/usr/bin/env bash

OUT_DIR="./src"
TS_OUT_DIR="./src"
IN_DIR="./protos"
PROTOC="$(npm bin)/grpc_tools_node_protoc"
PROTOC_GEN_TS_PATH="$(npm bin)/protoc-gen-ts"
PROTOC_GEN_GRPC_PATH="$(npm bin)/grpc_tools_node_protoc_plugin"

$PROTOC \
    -I="./" \
    --plugin=protoc-gen-ts=$PROTOC_GEN_TS_PATH \
    --plugin=protoc-gen-grpc=${PROTOC_GEN_GRPC_PATH} \
    --js_out=import_style=commonjs:$OUT_DIR \
    --grpc_out=grpc_js:$OUT_DIR \
    --ts_out=grpc_js:$TS_OUT_DIR \
    "$IN_DIR"/*.proto

但是当我在git bash 中运行此命令时:bash transpile-proto-ts.sh

它告诉我这个错误:

>--ts_out: protoc-gen-ts: %1 is not a valid Win32 application.

C:\Projects\GRPC\node_modules\grpc-tools\bin\protoc.js:41
    throw error;
    ^

Error: Command failed: C:\Projects\GRPC\node_modules\grpc-tools\bin\protoc.exe --plugin=protoc-gen-grpc=C:\Projects\GRPC\node_modules\grpc-tools\bin\grpc_node_plugin.exe --plugin=protoc-gen-grpc=./node_modules/.bin/grpc_tools_node_protoc_plugin --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --js_out=import_style=commonjs,binary:./protos --ts_out=./protos --grpc_out=./protos -I ./protos ./protos/example.proto
--ts_out: protoc-gen-ts: %1 is not a valid Win32 application.


    at ChildProcess.exithandler (node:child_process:397:12)
    at ChildProcess.emit (node:events:390:28)
    at maybeClose (node:internal/child_process:1064:16)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5) {
  killed: false,
  code: 1,
  signal: null,
  cmd: 'C:\\Projects\\GRPC\\node_modules\\grpc-tools\\bin\\protoc.exe --plugin=protoc-gen-grpc=C:\\Projects\\GRPC\\node_modules\\grpc-tools\\bin\\grpc_node_plugin.exe --plugin=protoc-gen-grpc=./node_modules/.bin/grpc_tools_node_protoc_plugin --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --js_out=import_style=commonjs,binary:./protos --ts_out=./protos --grpc_out=./protos -I ./protos ./protos/example.proto'
}

【问题讨论】:

  • 你是如何安装 protoc-gen-ts 插件的?

标签: javascript typescript npm grpc


【解决方案1】:

确保你的 sh 文件是可执行的

chmod 777 transpile-proto-ts.sh

我遇到了类似的问题,我使用了 YARN 而不是 NPM。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    • 2013-12-24
    • 1970-01-01
    • 2019-05-23
    • 2016-11-19
    • 2018-12-14
    • 1970-01-01
    相关资源
    最近更新 更多