{"record":{"id":"94047d6d1a7cabde","repo":"micro/go-micro","slug":"failed-to-create-jetstream-context","errorCode":null,"errorMessage":"Failed to create JetStream context","messagePattern":"Failed to create JetStream context","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"store/nats-js-kv/nats.go","lineNumber":85,"sourceCode":"\treturn n\n}\n\n// Init initializes the store. It must perform any required setup on the\n// backing storage implementation and check that it is ready for use,\n// returning any errors.\nfunc (n *natsStore) Init(opts ...store.Option) error {\n\tn.setOption(opts...)\n\n\t// Connect to NATS servers\n\tconn, err := n.nopts.Connect()\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Failed to connect to NATS Server\")\n\t}\n\n\t// Create JetStream context\n\tjs, err := conn.JetStream(n.jsopts...)\n\tif err != nil {\n\t\treturn errors.Wrap(err, \"Failed to create JetStream context\")\n\t}\n\n\tn.conn = conn\n\tn.js = js\n\n\t// Create default config if no configs present\n\tif len(n.kvConfigs) == 0 {\n\t\tif _, err := n.mustGetBucketByName(n.opts.Database); err != nil {\n\t\t\treturn err\n\t\t}\n\t}\n\n\t// Create kv store buckets\n\tfor _, cfg := range n.kvConfigs {\n\t\tif _, err := n.mustGetBucket(cfg); err != nil {\n\t\t\treturn err\n\t\t}\n\t}","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/nats-js-kv/nats.go#L67-L103","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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"],"exampleFix":"// before\nnats-server -p 4222\n// after\nnats-server -p 4222 -js","handlingStrategy":"validation","validationCode":"nc, err := nats.Connect(url)\nif err != nil { return err }\njs, err := nc.JetStream()\nif err != nil {\n    return fmt.Errorf(\"jetstream not available: %w\", err)\n}\ninfo, err := js.AccountInfo()\nif err != nil { return err }","typeGuard":null,"tryCatchPattern":"if err := st.Init(); err != nil {\n    if strings.Contains(err.Error(), \"Failed to create JetStream context\") {\n        return fmt.Errorf(\"NATS server lacks JetStream: %w\", err)\n    }\n    return err\n}","preventionTips":["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"],"tags":["nats","jetstream","initialization","connection"],"backgroundTag":"jetstream-not-enabled","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}