{"record":{"id":"f26bbafbd384f9d4","repo":"RocketChat/Rocket.Chat","slug":"invalid-mongo-options-environment-variable-must-b","errorCode":null,"errorMessage":"Invalid MONGO_OPTIONS environment variable: must be valid JSON.","messagePattern":"Invalid MONGO_OPTIONS environment variable: must be valid JSON\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"critical","filePath":"apps/meteor/packages/rocketchat-mongo-config/server/index.js","lineNumber":32,"sourceCode":"// This is fixed in Node 10, but this supports LTS versions\ntls.DEFAULT_ECDH_CURVE = 'auto';\n\nconst mongoConnectionOptions = {\n\t// add retryWrites=false if not present in MONGO_URL\n\t...(!process.env.MONGO_URL.includes('retryWrites') && { retryWrites: false }),\n\tignoreUndefined: false,\n\n\t// TODO ideally we should call isTracingEnabled(), but since this is a Meteor package we can't :/\n\tmonitorCommands: ['yes', 'true'].includes(String(process.env.TRACING_ENABLED).toLowerCase()),\n};\n\nconst mongoOptionStr = process.env.MONGO_OPTIONS;\nif (typeof mongoOptionStr !== 'undefined') {\n\ttry {\n\t\tconst mongoOptions = JSON.parse(mongoOptionStr);\n\t\tObject.assign(mongoConnectionOptions, mongoOptions);\n\t} catch (error) {\n\t\tthrow new Error('Invalid MONGO_OPTIONS environment variable: must be valid JSON.', { cause: error });\n\t}\n}\n\nif (Object.keys(mongoConnectionOptions).length > 0) {\n\tMongo.setConnectionOptions(mongoConnectionOptions);\n}\n\nprocess.env.HTTP_FORWARDED_COUNT = process.env.HTTP_FORWARDED_COUNT || '1';\n\n// Just print to logs if in TEST_MODE due to a bug in Meteor 2.5: TypeError: Cannot read property '_syncSendMail' of null\nif ((process.env.TEST_MODE === 'true' || process.env.TEST_MODE === 'api')) {\n\tEmail.sendAsync = async function _sendAsync(options) {\n\t\tconsole.log('Email.sendAsync', options);\n\t};\n} else if (process.env.NODE_ENV !== 'development') {\n\t// Send emails to a \"fake\" stream instead of print them in console in case MAIL_URL or SMTP is not configured\n\tconst stream = new PassThrough();\n\tstream.on('data', () => {});","sourceCodeStart":14,"sourceCodeEnd":50,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/packages/rocketchat-mongo-config/server/index.js#L14-L50","documentation":"The rocketchat-mongo-config Meteor package reads process.env.MONGO_OPTIONS, JSON.parses it, and Object.assigns the result into the Mongo connection options before calling Mongo.setConnectionOptions. If the variable is set but is not valid JSON, package startup throws this error immediately, chaining the JSON parse error as cause — the app fails to boot.","triggerScenarios":"Starting apps/meteor with MONGO_OPTIONS in a non-JSON format: key=value strings like 'ssl=true', Mongo URI query syntax 'ssl=true&sslValidate=false', single-quoted pseudo-JSON, trailing commas, or unquoted keys.","commonSituations":"Operators copying options syntax from MONGO_URL documentation into MONGO_OPTIONS; docker-compose/Kubernetes YAML quoting that mangles the value; stray whitespace/BOM in .env files.","solutions":["Set the variable to strict JSON, e.g. MONGO_OPTIONS='{\"ssl\":true,\"sslValidate\":false}'.","Validate before deploy: node -e 'JSON.parse(process.argv[1])' \"$MONGO_OPTIONS\".","Unset MONGO_OPTIONS entirely if no extra connection options are needed."],"exampleFix":"# before\nexport MONGO_OPTIONS=\"ssl=true&sslValidate=false\"\n\n# after\nexport MONGO_OPTIONS='{\"ssl\":true,\"sslValidate\":false}'","handlingStrategy":"validation","validationCode":"// startup/CI guard before launching the app\nif (process.env.MONGO_OPTIONS) {\n\ttry {\n\t\tJSON.parse(process.env.MONGO_OPTIONS);\n\t} catch (e) {\n\t\tconsole.error('MONGO_OPTIONS must be valid JSON, e.g. \\'{\"ssl\":true}\\'');\n\t\tprocess.exit(1);\n\t}\n}","typeGuard":"function isValidMongoOptionsEnv(value: string | undefined): boolean {\n\tif (value === undefined) return true;\n\ttry {\n\t\tconst parsed = JSON.parse(value);\n\t\treturn typeof parsed === 'object' && parsed !== null && !Array.isArray(parsed);\n\t} catch {\n\t\treturn false;\n\t}\n}","tryCatchPattern":null,"preventionTips":["Never use Mongo URI query syntax (ssl=true&...) in MONGO_OPTIONS — it must be a JSON object literal","Validate MONGO_OPTIONS in CI/entrypoint scripts so misconfiguration fails before deploy","Unset the variable when no extra Mongo connection options are needed"],"tags":["mongodb","environment","configuration","startup"],"backgroundTag":"invalid-env-var","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}