micro/go-micro · critical
Failed to create JetStream context
Error message
Failed to create JetStream context
What it means
Returned by Init when creating a JetStream context on an established NATS connection fails via conn.JetStream(). The library needs a JetStreamContext to manage KV buckets, so without it the store cannot operate. Usually indicates the NATS server has JetStream disabled or a supplied JS option is invalid.
Source
Thrown at store/nats-js-kv/nats.go:85
return n
}
// Init initializes the store. It must perform any required setup on the
// backing storage implementation and check that it is ready for use,
// returning any errors.
func (n *natsStore) Init(opts ...store.Option) error {
n.setOption(opts...)
// Connect to NATS servers
conn, err := n.nopts.Connect()
if err != nil {
return errors.Wrap(err, "Failed to connect to NATS Server")
}
// Create JetStream context
js, err := conn.JetStream(n.jsopts...)
if err != nil {
return errors.Wrap(err, "Failed to create JetStream context")
}
n.conn = conn
n.js = js
// Create default config if no configs present
if len(n.kvConfigs) == 0 {
if _, err := n.mustGetBucketByName(n.opts.Database); err != nil {
return err
}
}
// Create kv store buckets
for _, cfg := range n.kvConfigs {
if _, err := n.mustGetBucket(cfg); err != nil {
return err
}
}View on GitHub (pinned to 24529f1404)
Solutions
- Enable JetStream on the NATS server (start with -js or set jetstream.enabled=true) and restart it
- Verify the server version is >= 2.2 with jetstream enabled: run 'nats --server <url> server info' and check the jetstream block
- Review any []nats.JSOpt supplied via natsjskv.WithJSOptions/context for invalid settings and remove or correct them
- Upgrade github.com/nats-io/nats.go and the nats-server to compatible versions
Example fix
// before nats-server -p 4222 // after nats-server -p 4222 -js
Defensive patterns
Strategy: validation
Validate before calling
nc, err := nats.Connect(url)
if err != nil { return err }
js, err := nc.JetStream()
if err != nil {
return fmt.Errorf("jetstream not available: %w", err)
}
info, err := js.AccountInfo()
if err != nil { return err } Try / catch
if err := st.Init(); err != nil {
if strings.Contains(err.Error(), "Failed to create JetStream context") {
return fmt.Errorf("NATS server lacks JetStream: %w", err)
}
return err
} Prevention
- Always start nats-server with -js or jetstream.enabled=true in production configs
- Probe JetStream availability with a lightweight conn.JetStream()/AccountInfo check at startup before depending on the store
- Pin compatible nats-server and nats.go versions in CI
- Fail fast at deploy time with a health check that calls store.Init
When it happens
Trigger: Calling store.Init() (or any operation that lazily calls initConn) against a NATS server where JetStream is not enabled (jetstream disabled in nats-server config), or with invalid nats.JSOpt options passed via the jsOptionsKey context value.
Common situations: Connecting to a NATS server started without the -js flag or without 'jetstream { enabled: true }' in its config; pointing at an old NATS server (<2.2) that lacks JetStream; passing a malformed nats.JSOpt (e.g. bad api prefix or prepend/append option) through context options.
Related errors
- error connecting to nats cluster %v: %w
- Failed to connect to NATS Server
- source not found: %s
- error connecting to nats at %v with tls enabled (%v): %w
- error while obtaining JetStream context: %w
AI-assisted analysis of micro/go-micro@24529f1404 (2026-09-01).
Data as JSON: /api/errors/94047d6d1a7cabde.
Report an issue: GitHub.