(Node.js) --grpc_out: protoc-gen-grpc: %1 is not a valid Win32 application

帅比萌擦擦* 提交于 2020-05-16 01:42:17

问题


I want to compile my .proto file into stubs, but when entering the command:

`protoc -I=. ./protos/dummy.proto --js_out=import_style=commonjs,binary:./server --grpc_out=./server --plugin=protoc-gen-grpc=which grpc_tools_node_protoc_plugin

I got the following error :

--grpc_out: protoc-gen-grpc: %1 is not a valid Win32 application.

Thigs I have installed :

  • Windows 10
  • npm install -g grpc-tools
  • npm install google-protobuf
  • protoc

NOTE: I noticed there are a few similar questions already, but I didn't find one for Node.js, also the already asked questions all have different solutions.


回答1:


On Windows, I messed around with the grpc-tools for Node a bit and came across some issues. I installed the tools (and the TS plugin) via npm install grpc-tools grpc_tools_node_protoc_ts --save-dev to my local node_modules/.bin folder. Thanks to this post, I can provide these solutions. This was my original shell script

#!/usr/bin/env bash

PROTO_DIR="./src/grpc/proto"
PROTOC_GEN_TS_PATH="./node_modules/.bin/protoc-gen-ts"
GRPC_TOOLS_NODE_PROTOC_PLUGIN="./node_modules/.bin/grpc_tools_node_protoc_plugin"
GRPC_TOOLS_NODE_PROTOC="./node_modules/.bin/grpc_tools_node_protoc"

# Generate JS and corresponding TS d.ts codes for each .proto file using the grpc-tools for Node.
$GRPC_TOOLS_NODE_PROTOC \
    --plugin=protoc-gen-grpc="$GRPC_TOOLS_NODE_PROTOC_PLUGIN" \
    --plugin=protoc-gen-ts="$PROTOC_GEN_TS_PATH" \
    --js_out=import_style=commonjs,binary:"$PROTO_DIR" \
    --ts_out="$PROTO_DIR" \
    --grpc_out="$PROTO_DIR" \
    -I "$PROTO_DIR" \
    "$PROTO_DIR"/*.proto
  1. If you just provide the plugin by its name, e.g. --plugin=protoc-gen-grpc=./node_modules/.bin/grpc_tools_node_protoc_plugin, Windows will complain about an invalid Win32 application. You can solve this issue by adding the .cmd extension:

--plugin=protoc-gen-grpc=./node_modules/.bin/grpc_tools_node_protoc_plugin.cmd

  1. Unfortunately, the following issue '.' Not an internal or external command, or a runnable program or batch file arises, which indicates that Windows cannot resolve the relative path to the plugin. Therefore, you have to provide the absolute path, e.g.

--plugin=protoc-gen-grpc=C:/.../<project-dir>/node_modules/.bin/grpc_tools_node_protoc_plugin.cmd

Now the real magic happens:

Because of a simple typo (I typed --plugin=proto-gen-grpc instead of --plugin=protoc-gen-grpc), I figured out that, if you have grpc-tools and additional plugins installed in your local Node environment, you can simply omit --plugin. It seems that grpc_tools_node_protoc will automatically lookup the executables required to generate the code specified by the output flags --grpc_out, --js_out or --ts_out in ./node_modules/.bin/.

Therefore, I updated the following LOC in my script

$GRPC_TOOLS_NODE_PROTOC \
    --js_out=import_style=commonjs,binary:"$PROTO_DIR" \
    --ts_out="$PROTO_DIR" \
    --grpc_out="$PROTO_DIR" \
    -I "$PROTO_DIR" \
    "$PROTO_DIR"/*.proto

Maybe, others could share their experience and more clarifications to this issue.




回答2:


SIMPLE SOLUTION :

For me the "which" command was pointing to the wrong path so I removed it and replaced it with an absolute path to the plugin instead. It looks like this :

protoc -I=. ./protos/dummy.proto --js_out=import_style=commonjs,binary:./server --grpc_out=./server --plugin=protoc-gen-grpc=C:\Users\myUser\AppData\Roaming\npm\node_modules\grpc-tools\bin\grpc_node_plugin.exe

Explanation :

I am not sure why this error (--grpc_out: protoc-gen-grpc: %1 is not a valid Win32 application) was happening, but I have an theory... The "which" command pointed to the folder and not to the .exe file. How do I know this? When I try to only run the command (in the terminal)

which grpc_tools_node_protoc_plugin

It returns the folder

/c/Users/myUser/AppData/Roaming/npm/grpc_tools_node_protoc_plugin




回答3:


As grpc-tools has been installed globally (-g flag per OP), setting the plugin path beforehand works.

    GRPC_TOOLS_PLUGIN="$HOME/AppData/Roaming/npm/grpc_tools_node_protoc_plugin.cmd" && \
protoc -I=. ./protos/*.proto \
--js_out=import_style=commonjs,binary:./server \
--grpc_out=./server \
--plugin=protoc-gen-grpc=$GRPC_TOOLS_PLUGIN

Thanks to both answers before me for pointers!
Tested and found to be working on Windows 10 Pro and Git Bash.



来源:https://stackoverflow.com/questions/59447763/node-js-grpc-out-protoc-gen-grpc-1-is-not-a-valid-win32-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!