Skip to content

DTK0027: Shared State Not Found

Package: @vitejs/devtools

Message

Shared state of "{key}" is not found, please provide an initial value for the first time

Cause

The RpcSharedStateHost.get() method was called with a key that does not exist in the shared state map, and neither initialValue nor sharedState was provided in the options. When accessing a shared state key for the first time, the host needs an initial value to create the state instance. Subsequent calls with the same key will return the existing state without requiring options.

Example

ts
export default defineDevToolsPlugin({
  name: 'my-plugin',
  setup(context) {
    // Throws DTK0027 because the key doesn't exist yet and no initial value is given
    const state = await context.rpc.sharedState.get('my-plugin:config')
  },
})

Fix

Provide an initialValue when calling get() for a key that might not exist yet.

ts
const state = await context.rpc.sharedState.get('my-plugin:config', {
  initialValue: {
    theme: 'dark',
    showTimestamps: true,
  },
})

Alternatively, provide a pre-created sharedState instance:

ts
import { createSharedState } from '@vitejs/devtools-kit/utils/shared-state'

const myState = createSharedState({
  initialValue: { theme: 'dark' },
  enablePatches: false,
})

const state = await context.rpc.sharedState.get('my-plugin:config', {
  sharedState: myState,
})

After the first call with an initial value, subsequent calls to the same key do not need options:

ts
// This works because 'my-plugin:config' was already initialized above
const state = await context.rpc.sharedState.get('my-plugin:config')

Source

packages/core/src/node/rpc-shared-state.ts

Released under the MIT License.