{"record":{"id":"0e145e32630ea550","repo":"micro/go-micro","slug":"failed-to-connect-to-nats-server","errorCode":null,"errorMessage":"Failed to connect to NATS Server","messagePattern":"Failed to connect to NATS Server","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"critical","filePath":"store/nats-js-kv/nats.go","lineNumber":79,"sourceCode":"\t\tbuckets:     hashmap.New[string, nats.KeyValue](),\n\t\tstorageType: nats.FileStorage,\n\t}\n\n\tn.setOption(opts...)\n\n\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","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/micro/go-micro/blob/24529f140421a11a33b6999ab7944f2021cfd69c/store/nats-js-kv/nats.go#L61-L97","documentation":"natsStore.Init connects to the NATS servers via n.nopts.Connect() before obtaining a JetStream context; if the NATS connection cannot be established the store fails initialization with this message. The wrapped cause contains the NATS-specific reason (no servers, auth, TLS).","triggerScenarios":"store.Init(...) on the NATS JetStream KV store when no NATS server is reachable at the configured URLs, credentials are rejected, TLS handshake fails, or connection timeouts are hit.","commonSituations":"NATS server not running or wrong URL in NATS_URL; credentials/token expired; TLS certificate mismatch; network policy blocking port 4222; JetStream not enabled on the server (affects the next step).","solutions":["Verify NATS server is running and reachable (nats -s <url> server check or nc to 4222)","Fix the NATS connection options: URLs, credentials, TLS settings passed via store/nats options","Confirm credentials/token are valid and not expired","If connect fails intermittently, enable/rely on NATS reconnect and retry Init"],"exampleFix":"// before\ns := natsjs.NewStore(store.Nodes(\"nats://wrong:4222\"))\n// after\ns := natsjs.NewStore(store.Nodes(\"nats://nats:4222\"))\ns.Init(store.Namespace(\"micro\"))","handlingStrategy":"try-catch","validationCode":"// check NATS reachability before Init\nconn, err := nats.Connect(\"nats://nats:4222\", nats.Timeout(2*time.Second))\nif err != nil {\n    return fmt.Errorf(\"nats unreachable: %w\", err)\n}\nconn.Close()","typeGuard":null,"tryCatchPattern":"if err := st.Init(opts...); err != nil {\n    if strings.Contains(err.Error(), \"Failed to connect to NATS Server\") {\n        log.Printf(\"nats connect failed: %+v\", err)\n        time.Sleep(backoff)\n        return st.Init(opts...)\n    }\n    return err\n}","preventionTips":["Configure NATS URLs explicitly via store Nodes option instead of defaults","Verify credentials/TLS options match the server config","Confirm JetStream is enabled on the NATS server","Add startup retries with backoff for NATS availability"],"tags":["nats","store","jetstream","connection"],"backgroundTag":"nats-connection-failed","analyzedSha":"24529f140421a11a33b6999ab7944f2021cfd69c","analyzedAt":"2026-09-01T02:52:24.923Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T05:18:18.240Z"}