siyuan-note/siyuan · error
globalThis.siyuan.event not found
Error message
globalThis.siyuan.event not found
What it means
dispatchEvent resolves globalThis.siyuan.event in the plugin's goja runtime before delivering an event. getJsContextValue returns nil without error when the path resolves to nothing, and dispatchEvent then fails with this explicit error because events cannot be dispatched without the event hub.
Solutions
- Ensure EnableSiyuanModule ran successfully before dispatching events
- Check earlier plugin-load errors (e.g. 'failed to inject global context') that left siyuan partially injected
- Verify plugin code does not overwrite or delete globalThis.siyuan.event
- Validate the runtime is the sandbox-constructed one, not a bare goja.Runtime
Example fix
// before
rt := goja.New()
dispatchEvent(p, rt, "someEvent", data) // no siyuan.event -> error
// after
rt := goja.New()
if err := EnableSiyuanModule(p, rt); err != nil { return err }
dispatchEvent(p, rt, "someEvent", data) Defensive patterns
Strategy: fallback
Validate before calling
event, err := getJsContextValue(rt, []any{"siyuan", "event"})
if err != nil || event == nil {
return fmt.Errorf("siyuan.event unavailable; was EnableSiyuanModule run?")
} Type guard
func eventHubReady(rt *goja.Runtime) bool {
v, err := getJsContextValue(rt, []any{"siyuan", "event"})
return err == nil && v != nil && !goja.IsUndefined(v) && !goja.IsNull(v)
} Try / catch
if err := dispatchEvent(p, rt, name, data); err != nil {
log.Warnf("event %s not delivered: %v", name, err)
} Prevention
- Always run EnableSiyuanModule to completion before dispatching events
- Surface earlier injection errors instead of ignoring them
- Forbid plugin scripts from reassigning globalThis.siyuan namespace
When it happens
Trigger: Dispatching a plugin event when the siyuan module was never injected (EnableSiyuanModule skipped or failed) or the runtime defines siyuan without an event object, so getJsContextValue(rt, ["siyuan","event"]) returns nil.
Common situations: Plugin sandbox initialized partially — global injection failed earlier; plugin code deleted/reassigned globalThis.siyuan.event; dispatchEvent invoked on a runtime created outside the standard sandbox bootstrap.
Understand the failure class
Background: 'Could not be found', 'does not exist', 'not found in database': the resource-not-found family when an ID, slug, key, or URI lookup comes back empty — this error's family across 20 libraries.
Related errors
- globalThis.siyuan.event is not an object
- globalThis.siyuan.event is
- expected promise object, got %T
- failed to enable extend modules
- failed to inject global context
AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19).
Data as JSON: /api/errors/81c00ad491628e7f.
Report an issue: GitHub.
Appendix: source
Thrown at kernel/plugin/sandbox.go:337
}
value = cursor
return
}
// dispatchEvent calls the globalThis.siyuan.event.on hook with the given event object.
func dispatchEvent(p *KernelPlugin, rt *goja.Runtime, e any) (async bool, err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("goja panic during dispatchEvent: %v", r)
}
}()
event, err := getJsContextValue(rt, []any{"siyuan", "event"})
if err != nil {
return
}
if event == nil {
err = fmt.Errorf("globalThis.siyuan.event not found")
return
}
if goja.IsUndefined(event) || goja.IsNull(event) {
err = fmt.Errorf("globalThis.siyuan.event is %s", event.String())
return
}
eventObj := event.ToObject(rt)
if eventObj == nil {
err = fmt.Errorf("globalThis.siyuan.event is not an object")
return
}
handlerValue := eventObj.Get("handler")
handler, ok := goja.AssertFunction(handlerValue)
if !ok {
return
}View on GitHub (pinned to 9f775e8a12)