Registry / web-framework / vite-dev-rpc

vite-dev-rpc

JSON →
library1.1.0jsnpmunverified

vite-dev-rpc provides a remote procedure call (RPC) mechanism for seamless client-server communication within Vite plugins. It enables developers to define functions on the server-side (Vite plugin context) and invoke them directly from the client-side code, facilitating complex interactions during development. The package is currently at version 1.1.0, actively maintained, with releases primarily focused on supporting new major versions of Vite. It leverages `birpc` for the underlying message-based RPC, `vite-hot-client` to access `import.meta.hot` at runtime, and Vite's `import.meta.hot.send` API for server-client messaging. Its key differentiator is its tight integration with the Vite development server's HMR context, making it a robust solution for enhancing developer tools and features within Vite projects.

npm install vite-dev-rpc
INSTALL
IMPORT
SIG · VITE-DEV-RPC
V
vite-dev-rpc
web-frameworkjavascriptv1.1.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

createRPCServer
✓ import { createRPCServer } from 'vite-dev-rpc'
✗ const { createRPCServer } = require('vite-dev-rpc')
ESM-only package, CommonJS require is not supported. Use named import for server-side RPC setup in Vite plugins.
createRPCClient
✓ import { createRPCClient } from 'vite-dev-rpc'
✗ import createRPCClient from 'vite-dev-rpc'
ESM-only. This function is typically used in client-side code to connect to the RPC server exposed by the Vite plugin. It is a named export.
ViteDevRpcOptions
✓ import type { ViteDevRpcOptions } from 'vite-dev-rpc'
TypeScript type import for configuring the RPC client or server. Always use `import type`.

This quickstart demonstrates how to set up a `vite-dev-rpc` server within a Vite plugin and establish a client connection from the browser, allowing remote procedure calls for `multiply` and `greet` methods.

import { defineConfig } from 'vite'; import { createRPCServer, createRPCClient } from 'vite-dev-rpc'; // --- Vite Plugin (Server-side) --- const myVitePlugin = () => { const rpc = createRPCServer('my-rpc-channel', { multiply(a: number, b: number) { console.log('Server received multiplication request'); return a * b; }, greet(name: string) { console.log('Server received greeting request'); return `Hello, ${name} from Vite server!`; }, }); return { name: 'my-rpc-plugin', configureServer(server) { server.ws.on('connection', (socket) => { // Expose RPC methods to the client via Vite's WebSocket rpc.update(socket); }); }, // In case you need to send messages from server to client // You can access rpc.send() or rpc.broadcast() here or from a configured client }; }; export default defineConfig({ plugins: [myVitePlugin()], }); // --- Client-side (e.g., in a .js or .ts file loaded by Vite) --- async function setupClientRPC() { // Ensure import.meta.hot is available if (import.meta.hot) { const clientRpc = createRPCClient<typeof serverMethods>('my-rpc-channel', import.meta.hot); try { const result = await clientRpc.multiply(5, 7); console.log('Client received multiplication result:', result); // Expected: 35 const greeting = await clientRpc.greet('Vite User'); console.log('Client received greeting:', greeting); // Expected: "Hello, Vite User from Vite server!" } catch (error) { console.error('RPC client error:', error); } } else { console.warn('Vite HMR is not available, RPC client cannot be initialized.'); } } // Define types for server methods for client-side type safety interface serverMethods { multiply(a: number, b: number): number; greet(name: string): string; } setupClientRPC();
Debug
Known issues
breakingvite-dev-rpc is tightly coupled with Vite's internal HMR client and server APIs. Ensure your `vite` peer dependency matches the supported versions (`^2.9.0 || ^3.0.0-0 || ^4.0.0-0 || ^5.0.0-0 || ^6.0.1 || ^7.0.0-0`). Mismatches can lead to `import.meta.hot` context issues or WebSocket errors.
fix
Check your `vite` version and update your `package.json` to install a compatible version, then run `npm install` or `yarn install`.
affects: >=0.0.1
gotchaThe `createRPCClient` function requires `import.meta.hot` to be available. This object is only present in a module that is part of the Vite HMR graph. If you try to use `createRPCClient` in a non-HMR context (e.g., a vanilla Node.js script not processed by Vite), it will fail or `import.meta.hot` will be undefined.
fix
Ensure the client-side code where `createRPCClient` is called is served and processed by Vite's development server. Wrap client RPC setup in a check like `if (import.meta.hot) { ... }`.
affects: >=0.0.1
gotchaWhen migrating to newer Vite versions, always check `vite-dev-rpc` release notes. Although `v1.0.7` adopted 'Epoch SemVer' without immediate breaking changes, subsequent Vite updates (e.g., Vite 3, 5, 7 support in `v0.1.0`, `v0.1.4`, `v1.1.0` respectively) might necessitate updating `vite-dev-rpc` to ensure compatibility with underlying Vite HMR API changes.
fix
Periodically update `vite-dev-rpc` to its latest version when upgrading Vite to maintain compatibility and benefit from bug fixes and new features.
affects: >=0.0.1
Errors
Common errors & fixes
TypeError: Cannot read properties of undefined (reading 'on') or 'send'
The `createRPCServer` or `createRPCClient` is not correctly initialized with the Vite WebSocket server or `import.meta.hot` context, or the `vite` peer dependency is incompatible.
fix
Verify that `server.ws` (for `createRPCServer`) or `import.meta.hot` (for `createRPCClient`) is correctly passed and that your `vite` version satisfies `vite-dev-rpc`'s peer dependency requirements.
Error: [vite-dev-rpc] Channel "my-channel" is already registered. Make sure you use unique channel names.
Attempting to create multiple RPC servers or clients with the same channel name in the same context, leading to a naming collision.
fix
Ensure each `createRPCServer` and `createRPCClient` pair uses a unique `channel` string identifier across your application to prevent conflicts.
RPC Client error: TypeError: rpcMethod is not a function
The client attempted to call an RPC method that was not defined or properly exposed by the RPC server, or there's a type mismatch in the method signature.
fix
Double-check that the method name exists on the server-side RPC definition (`createRPCServer` methods object) and that the client is calling it with the correct arguments and type signature.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies
viterequiredPeer dependency for Vite plugin functionality and HMR context.
Agent activity
20 hits · last 30 days
node
16
OpenAI (training)
1
Resources
vite-dev-rpc — npm install vite-dev-rpc · libregistry