@bufbuild/protoc-gen-grpc-es

中文文档
0.0.1发布于 2026/08/03Apache-2.0近 30 天下载 4833

Protobuf code generator for @grpc/grpc-js

原始使用文档
文档目录5 节
  1. Getting started
  2. Installation
  3. Generating code
  4. Usage
  5. Plugin options

The Buf logo

@bufbuild/protoc-gen-grpc-es

NPM Version NPM License Slack

protoc-gen-grpc-es generates well-typed, idiomatic gRPC stubs for Protobuf-ES (@bufbuild/protobuf), the ergonomic and modern Protobuf library for JavaScript and TypeScript.

Plugs into your existing @grpc/grpc-js channels and servers.

It's a compatibility layer for projects already built on @grpc/grpc-js. The generated clients and servers plug into your existing gRPC channels and servers, but serialize Protobuf-ES messages directly. This lets you upgrade to a better Protobuf package without touching your RPC stack.

[!TIP] For new projects, use Connect-ES instead. Connect speaks the gRPC and gRPC-Web protocols in addition to its own, so existing gRPC clients can call a Connect server unchanged. But you also get plain HTTP APIs you can curl, first-class streaming, and generated clients for every major language, including your frontend. protoc-gen-grpc-es only exists so current gRPC codebases already built on @grpc/grpc-js can get the improvements of Protobuf-ES today without a full rewrite.

Getting started

Installation

To install the runtime libraries and the protoc plugins, run:

npm install @bufbuild/protobuf @grpc/grpc-js
npm install --save-dev @bufbuild/protoc-gen-es @bufbuild/protoc-gen-grpc-es @types/node

Generating code

We recommend using buf to generate code. Add a new buf.gen.yaml configuration file:

# Learn more: https://buf.build/docs/configuration/v2/buf-gen-yaml
version: v2
inputs:
  - directory: proto
plugins:
  - local: protoc-gen-es
    out: src/gen
    opt: target=ts
  - local: protoc-gen-grpc-es
    out: src/gen
    opt: target=ts

Place your .proto files in the ./proto directory, then run:

npx buf generate

Usage

For every Protobuf file that contains services, the plugin generates a _grpc.ts file next to the _pb.ts file from protoc-gen-es. For example, the following service:

syntax = "proto3";
package example;

service ElizaService {
  rpc Say(SayRequest) returns (SayResponse);
}

message SayRequest {
  string sentence = 1;
}

message SayResponse {
  string sentence = 1;
}

generates the following exports in eliza_grpc.ts:

  • ElizaServiceDefinition: The @grpc/grpc-js service definition.
  • ElizaServiceServer: An interface for the server implementation.
  • ElizaServiceClient: A client with a typed method for every RPC.

To implement a server:

import * as grpc from "@grpc/grpc-js";
import { ElizaServiceDefinition, type ElizaServiceServer } from "./gen/eliza_grpc.js";

const implementation: ElizaServiceServer = {
  say(call, callback) {
    callback(null, { sentence: `You said: ${call.request.sentence}` });
  },
};

const server = new grpc.Server();
server.addService(ElizaServiceDefinition, implementation);
server.bindAsync("localhost:50051", grpc.ServerCredentials.createInsecure(), () => {});

To call it with a client:

import * as grpc from "@grpc/grpc-js";
import { ElizaServiceClient } from "./gen/eliza_grpc.js";

const client = new ElizaServiceClient("localhost:50051", grpc.credentials.createInsecure());
client.say({ sentence: "Hi" }, (error, response) => {
  if (error) {
    throw error;
  }
  console.log(response?.sentence);
});

Plugin options

protoc-gen-grpc-es supports the same options as protoc-gen-es:

OptionValuesDefaultDescription
targetjs, ts, dts (combine with +)js+dtsWhich files to generate.
import_extensionnone, js, tsnoneFile extension to use in import paths.
js_import_stylemodule, legacy_commonjsmoduleUse ECMAScript import/export or CommonJS require.
keep_empty_filestrue, falsefalseKeep generated files even if they contain no services.
ts_nochecktrue, falsefalseGenerate a // @ts-nocheck annotation at the top of each file.
elide_plugin_versiontrue, falsefalseOmit the plugin version from generated file headers.

Make sure to use the same target and import_extension values for both plugins, so that the generated _grpc files can import from the generated _pb files.

版本与发布记录

@bufbuild/protoc-gen-grpc-es 共发布 1 个版本,首次发布于 2026/08/03;过去 12 个月发布了 1 个版本。

版本发布日期距今
0.0.1latest2026/08/033 个月前

关于 @bufbuild/protoc-gen-grpc-es 的常见问题

@bufbuild/protoc-gen-grpc-es 是什么?

Protobuf code generator for @grpc/grpc-js

如何安装 @bufbuild/protoc-gen-grpc-es?

在项目目录运行 npm install @bufbuild/protoc-gen-grpc-es,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 protoc-gen-grpc-es,可以用 npx protoc-gen-grpc-es 直接运行。

@bufbuild/protoc-gen-grpc-es 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/bufbuild__protoc-gen-grpc-es 是否存在。

@bufbuild/protoc-gen-grpc-es 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=20。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@bufbuild/protoc-gen-grpc-es 可以商用吗?

@bufbuild/protoc-gen-grpc-es 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@bufbuild/protoc-gen-grpc-es 的下载量有多少?

根据 npm 官方下载统计,@bufbuild/protoc-gen-grpc-es 在 2026/10/09 – 2026/11/07 这 30 天内被下载 4,833 次,日均约 161 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@bufbuild/protoc-gen-grpc-es 还在维护吗?

最新版本 0.0.1 发布于 2026/08/03(3 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2026/08/03。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。