gRPC Protocol Buffer Inspector
grpc-inspect is a utility module for Node.js designed to parse and inspect gRPC Protocol Buffer definitions, generating a "friendlier" JavaScript object representation with helper methods. This allows developers to programmatically explore service methods, message structures, and field details loaded from `.proto` files. The current stable version, 0.6.0, was last published over seven years ago. The package is effectively abandoned, relying on the legacy `grpc` package for protobuf loading. Consequently, its utility is severely limited in modern gRPC environments and with current Node.js versions, making it unsuitable for new projects and likely problematic for existing ones that have upgraded their gRPC dependencies. Its primary differentiator was providing an easier-to-traverse descriptor object than the raw output of `grpc.load`.
Common errors
-
TypeError: grpc.load is not a function
cause You are likely using `@grpc/grpc-js` which does not expose `grpc.load` directly. Instead, `proto-loader` is used for loading `.proto` files.fixIf you are using `@grpc/grpc-js`, you cannot use `grpc-inspect` directly as it depends on the old `grpc` package. Consider migrating your protobuf loading to `@grpc/proto-loader` and implement custom inspection logic, or use tools designed for modern gRPC. -
Error: Cannot find module 'grpc'
cause The `grpc` package, which `grpc-inspect` depends on, is not installed or cannot be resolved in your project.fixInstall the deprecated `grpc` package: `npm install grpc`. Be aware that this package is unmaintained and may have compatibility issues with newer Node.js versions. -
SyntaxError: Named export 'grpcInspect' not found. The requested module 'grpc-inspect' is a CommonJS module, which may not support all module.exports as named exports.
cause You are attempting to `import grpcInspect from 'grpc-inspect'` in an ES Module context, but `grpc-inspect` is a CommonJS module and does not provide named ESM exports.fixUse the CommonJS `require` syntax: `const grpcInspect = require('grpc-inspect');`. If you must use it in an ESM file, you can try `import grpcInspect from 'grpc-inspect';` after configuring your bundler or Node.js to handle CJS imports, or use dynamic import `const grpcInspect = await import('grpc-inspect');`.
Warnings
- breaking The `grpc-inspect` package heavily relies on the deprecated `grpc` npm package for loading `.proto` files. The `grpc` package has been superseded by `@grpc/grpc-js` and `@grpc/proto-loader`, which have different APIs and behaviors. Using `grpc-inspect` with modern gRPC setups (e.g., `@grpc/grpc-js`) will likely require significant refactoring or may not be directly compatible.
- breaking This package is effectively abandoned, with its last update over seven years ago. It is not maintained and may have unaddressed bugs, security vulnerabilities, or incompatibilities with newer Node.js versions or underlying system libraries.
- gotcha The package uses CommonJS (`require`) syntax and does not officially support ES Modules (`import`). Attempting to `import` it directly in an ESM context will result in errors.
- gotcha Loading `.proto` files requires careful path resolution, especially when using `grpc.load`. Relative paths might not resolve as expected in different execution environments.
Install
-
npm install grpc-inspect -
yarn add grpc-inspect -
pnpm add grpc-inspect
Imports
- grpcInspect
import grpcInspect from 'grpc-inspect';
const grpcInspect = require('grpc-inspect'); - descriptor.namespaceNames
import { namespaceNames } from 'grpc-inspect';const gi = require('grpc-inspect'); const proto = grpc.load(pbpath); const d = gi(proto); const namespaces = d.namespaceNames();
Quickstart
const grpcInspect = require('grpc-inspect');
const grpc = require('grpc');
const path = require('path');
const fs = require('fs');
// Create a dummy .proto file for demonstration
const pbpath = path.resolve(__dirname, 'helloworld.proto');
const protoContent = `
syntax = "proto3";
package helloworld;
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
string name = 1;
}
message HelloReply {
string message = 1;
}`;
fs.writeFileSync(pbpath, protoContent);
try {
// Load the protobuf definition using the deprecated 'grpc' package
const proto = grpc.load(pbpath);
// Inspect the loaded protobuf definition
const descriptor = grpcInspect(proto);
console.log('--- Namespaces ---');
console.dir(descriptor.namespaceNames());
console.log('\n--- Services in helloworld namespace ---');
console.dir(descriptor.serviceNames('helloworld'));
console.log('\n--- Greeter service methods ---');
console.dir(descriptor.methods('Greeter'));
console.log('\n--- Full Descriptor Object (truncated) ---');
// console.dir(descriptor, { depth: null }); // Uncomment for full object
console.dir(descriptor.namespaces.helloworld.messages.HelloRequest); // Example message inspection
} catch (error) {
console.error('Error loading or inspecting proto:', error.message);
console.error('This package uses the deprecated `grpc` module, which may cause issues with modern Node.js versions or `@grpc/grpc-js`.');
} finally {
// Clean up the dummy proto file
fs.unlinkSync(pbpath);
}