Install & Compatibility
Where this runs
No compatibility data collected yet for this library.
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
createServer
✓ import { createServer } from 'basic-grpc'
✗ const { createServer } = require('basic-grpc')
ESM-only; CommonJS require may not work depending on bundler. Use import statement.
createClient
✓ import { createClient } from 'basic-grpc'
✗ const createClient = require('basic-grpc').createClient
ESM-only; CommonJS require may not work depending on bundler. Use import statement.
RPCError
✓ import { RPCError } from 'basic-grpc'
✗ const RPCError = require('basic-grpc').RPCError
ESM-only; CommonJS require may not work depending on bundler. Use import statement.
createServerCreds
✓ import { createServerCreds } from 'basic-grpc'
✗ const createServerCreds = require('basic-grpc').createServerCreds
ESM-only; CommonJS require may not work depending on bundler. Use import statement.
createClientCreds
✓ import { createClientCreds } from 'basic-grpc'
✗ const createClientCreds = require('basic-grpc').createClientCreds
ESM-only; CommonJS require may not work depending on bundler. Use import statement.
ISetup
✓ import { ISetup } from 'basic-grpc'
TypeScript only; used for typing the server setup configuration.
IProtoService
✓ import { IProtoService } from 'basic-grpc'
TypeScript only; used for typing a service definition.
Creates a gRPC server and client with a simple login service, demonstrating service definition, error handling, and client invocation.
import { createServer, createClient, RPCError, IProtoService, ISetup } from 'basic-grpc';
import { join } from 'path';
// Server setup
const loginService: IProtoService<{ login: Function }> = {
protoFile: 'login.proto',
packageName: 'login',
serviceName: 'Login',
handlers: {
login: async ({ username, password }: { username: string; password: string }) => {
if (password === 'secret') return { token: 'abc123' };
throw new RPCError(22, 'Invalid credentials');
}
}
};
const serverConfig: ISetup = {
protoRoot: join(__dirname, 'protos'),
services: [loginService]
};
const server = createServer({
gRPCSetup: serverConfig,
domain: 'localhost:50051'
});
server.start();
// Client setup
const client = createClient<{ login: Function }>({
url: 'localhost',
port: '50051',
protoPath: join(__dirname, 'protos', 'login.proto'),
packageName: 'login',
serviceName: 'Login'
});
async function main() {
const result = await client.login({ username: 'user', password: 'secret' });
console.log(result.token);
}
main();
Errors
Common errors & fixes
Error: Cannot find module 'grpc'
Missing dependency: 'grpc' is required but not installed (or failing to compile).
fixRun npm install grpc. If native compilation fails, ensure build tools are installed (e.g., build-essential on Linux, windows-build-tools on Windows).
TypeError: login is not a function
Client method not correctly typed or the service definition mismatches. The createClient generic may not match the actual RPC method name.
fixEnsure the TypeScript interface used with createClient has a property matching the RPC method name (e.g., login). Check proto file and client declaration.
Error: 20 UNIMPLEMENTED: Method not found: /login.Login/login
Service name, package name, or proto path mismatched between server and client definitions.
fixVerify that protoFile, packageName, and serviceName in server match protoPath, packageName, and serviceName in client. Also ensure the .proto file is correctly loaded.
TypeError: grpc.loadPackageDefinition is not a function
Incompatible version of '@grpc/proto-loader'. The package expects an older API.
fixInstall '@grpc/proto-loader@0.5.x' (version 0.6.x changes API). Use npm install @grpc/proto-loader@0.5.0.
Audit
Dependencies
grpcrequiredCore gRPC library used for server and client communication
@grpc/proto-loaderrequiredLoads .proto files and generates service definitions