{"record":{"id":"bece3f461fe80454","repo":"louislam/dockge","slug":"invalid-db-config-json-it-must-be-an-object","errorCode":null,"errorMessage":"Invalid db-config.json, it must be an object","messagePattern":"Invalid db-config\\.json, it must be an object","errorType":"console","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"backend/database.ts","lineNumber":65,"sourceCode":"        await Database.connect();\n        log.info(\"server\", \"Connected to the database\");\n\n        // Patch the database\n        await Database.patch();\n    }\n\n    /**\n     * Read the database config\n     * @throws {Error} If the config is invalid\n     * @typedef {string|undefined} envString\n     * @returns {{type: \"sqlite\"} | {type:envString, hostname:envString, port:envString, database:envString, username:envString, password:envString}} Database config\n     */\n    static readDBConfig() : DBConfig {\n        const dbConfigString = fs.readFileSync(path.join(this.server.config.dataDir, \"db-config.json\")).toString(\"utf-8\");\n        const dbConfig = JSON.parse(dbConfigString);\n\n        if (typeof dbConfig !== \"object\") {\n            throw new Error(\"Invalid db-config.json, it must be an object\");\n        }\n\n        if (typeof dbConfig.type !== \"string\") {\n            throw new Error(\"Invalid db-config.json, type must be a string\");\n        }\n        return dbConfig;\n    }\n\n    /**\n     * @typedef {string|undefined} envString\n     * @param dbConfig the database configuration that should be written\n     * @returns {void}\n     */\n    static writeDBConfig(dbConfig : DBConfig) {\n        fs.writeFileSync(path.join(this.server.config.dataDir, \"db-config.json\"), JSON.stringify(dbConfig, null, 4));\n    }\n\n    /**","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/louislam/dockge/blob/f809ae192b571944ad773e9866d3e67064ae8043/backend/database.ts#L47-L83","documentation":"Database.readDBConfig reads <dataDir>/db-config.json, JSON.parses it, and requires the result to be an object. If the parsed value is an array, string, number, or null (JSON 'null' is typeof 'object' but here a non-plain object fails downstream; arrays/null pass typeof but the check catches primitives), the method throws 'Invalid db-config.json, it must be an object'. Note connect() catches this and falls back to a default sqlite config, logging a warning.","triggerScenarios":"db-config.json contains 'null', a JSON array like [], a quoted string, or any primitive instead of an object like {\"type\":\"sqlite\"}.","commonSituations":"Hand-edited or corrupted config file; empty file truncated by a crash (JSON.parse would throw first, caught by connect); a migration/backup tool overwrote the file; docker volume mounting a wrong file.","solutions":["Restore db-config.json to an object: {\"type\": \"sqlite\"}","Delete the file and let Dockge regenerate the default sqlite config on next start","Validate the JSON structure: typeof JSON.parse(content) === 'object' && !Array.isArray(...) before replacing it"],"exampleFix":"// before (db-config.json)\n[]\n// after\ntype: sqlite -> {\"type\": \"sqlite\"}","handlingStrategy":"validation","validationCode":"const fs = require(\"fs\");\nconst raw = JSON.parse(fs.readFileSync(dataDir + \"/db-config.json\", \"utf-8\"));\nif (typeof raw !== \"object\" || raw === null || Array.isArray(raw)) { fs.writeFileSync(dataDir + \"/db-config.json\", JSON.stringify({ type: \"sqlite\" })); }","typeGuard":"function isDBConfig(v: unknown): v is { type: string } { return typeof v === \"object\" && v !== null && !Array.isArray(v) && typeof (v as any).type === \"string\"; }","tryCatchPattern":"try { Database.connect(); } catch (e) { if (String(e.message).includes(\"db-config.json\")) { /* reset config to {type:'sqlite'} and retry */ } else { throw e; } }","preventionTips":["Never hand-edit db-config.json without validating JSON afterwards","Back up the data dir before upgrades or migrations","Ensure atomic writes (write temp file + rename) so crashes cannot truncate the config"],"tags":["database","config","validation","json"],"backgroundTag":"invalid-config-file","analyzedSha":"f809ae192b571944ad773e9866d3e67064ae8043","analyzedAt":"2026-08-31T19:13:50.919Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}