{"record":{"id":"22856cdebbdb8f7b","repo":"neoclide/coc.nvim","slug":"error-parsing-json-module-cachekey-e-as-err","errorCode":null,"errorMessage":"Error parsing JSON module ${cacheKey}: ${(e as Error).message}","messagePattern":"Error parsing JSON module (.+?): (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/extension/loader.ts","lineNumber":292,"sourceCode":"    const module = this.createModule(cacheKey, parent)\n    module.exports = nodeModule.exports\n    module.loaded = true\n    return module.exports\n  }\n\n  /**\n   * Load a JSON module synchronously and cache the parsed value per runtime.\n   */\n  public loadJson(filename: string, parent?: ExtensionCommonJSModule): unknown {\n    const cacheKey = this.normalizeFilename(filename)\n    const cached = this.runtime.cjsModules.get(cacheKey)\n    if (cached) return cached.exports\n    const source = fs.readFileSync(cacheKey, 'utf8')\n    let value: unknown\n    try {\n      value = JSON.parse(stripBOM(source))\n    } catch (e) {\n      throw new Error(`Error parsing JSON module ${cacheKey}: ${(e as Error).message}`, { cause: e })\n    }\n    const module = this.createModule(cacheKey, parent)\n    module.exports = value\n    module.loaded = true\n    return module.exports\n  }\n\n  public loadBuiltin(request: string): unknown {\n    if (request === 'process' || request === 'node:process') {\n      return (this.runtime.context as any).process\n    }\n    if (request === 'console' || request === 'node:console') {\n      return getConsoleFacade(this.runtime)\n    }\n    return require(request)\n  }\n\n  /**","sourceCodeStart":274,"sourceCodeEnd":310,"githubUrl":"https://github.com/neoclide/coc.nvim/blob/50e974d9692461a69147d5cab146a8d3e439abe4/src/extension/loader.ts#L274-L310","documentation":"loadJson in src/extension/loader.ts reads a JSON module file synchronously and calls JSON.parse on its BOM-stripped contents. When JSON.parse throws (the file is not valid JSON), the raw parse error is re-wrapped as `Error parsing JSON module <path>: <message>` with the original error attached as `cause`. The cacheKey (absolute file path) is embedded so the developer can locate the offending file.","triggerScenarios":"Calling load()/loadJson for a single-file JSON extension module whose file contains malformed JSON: syntax errors, trailing commas, comments, truncated file, or a file that is actually JavaScript but has a .json-style registration.","commonSituations":"Hand-edited extension JSON with a missing comma or quote; a file saved with a UTF-8 BOM in a pipeline that didn't strip it (stripBOM handles that, so usually real syntax errors); a truncated download/copy of the module; writing JSON by string concatenation instead of JSON.stringify.","solutions":["Validate the file: run `node -e \"JSON.parse(require('fs').readFileSync('<path>','utf8'))\"` and fix the syntax error reported at the given position","Regenerate the JSON module file (reinstall or re-export it with JSON.stringify instead of hand-editing)","Check the `cause` property of the thrown error for the exact line/column of the parse failure","Ensure the file is UTF-8 encoded and complete (compare file size/checksum with a known-good copy)"],"exampleFix":"// before (hand-edited package-like module with trailing comma)\n{ \"name\": \"my-ext\", \"version\": \"1.0.0\", }\n// after\n{ \"name\": \"my-ext\", \"version\": \"1.0.0\" }","handlingStrategy":"validation","validationCode":"const src = fs.readFileSync(p, 'utf8').replace(/^\\uFEFF/, '')\ntry { JSON.parse(src) } catch (e) { throw new Error(`Invalid JSON module ${p}: ${e.message}`) }","typeGuard":null,"tryCatchPattern":"try {\n  await loader.load(path)\n} catch (e) {\n  if (e.message.startsWith('Error parsing JSON module')) {\n    logger.error(`Bad JSON at ${path}:`, e.cause ?? e)\n  } else throw e\n}","preventionTips":["Validate extension JSON files with JSON.parse or a linter in CI before distributing","Always write JSON with JSON.stringify, never string concatenation","Keep files UTF-8 without manual BOM edits","Log the `cause` of wrapped parse errors for exact position"],"tags":["json","parse-error","extension-loading"],"backgroundTag":"json-parse-error","analyzedSha":"50e974d9692461a69147d5cab146a8d3e439abe4","analyzedAt":"2026-08-31T11:17:23.966Z","schemaVersion":2},"datasetVersion":"2026-09-01T08:17:40.651Z"}