neoclide/coc.nvim · error

Error parsing JSON module ${cacheKey}: ${(e as Error).messag

Error message

Error parsing JSON module ${cacheKey}: ${(e as Error).message}

What it means

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.

Source

Thrown at src/extension/loader.ts:292

    const module = this.createModule(cacheKey, parent)
    module.exports = nodeModule.exports
    module.loaded = true
    return module.exports
  }

  /**
   * Load a JSON module synchronously and cache the parsed value per runtime.
   */
  public loadJson(filename: string, parent?: ExtensionCommonJSModule): unknown {
    const cacheKey = this.normalizeFilename(filename)
    const cached = this.runtime.cjsModules.get(cacheKey)
    if (cached) return cached.exports
    const source = fs.readFileSync(cacheKey, 'utf8')
    let value: unknown
    try {
      value = JSON.parse(stripBOM(source))
    } catch (e) {
      throw new Error(`Error parsing JSON module ${cacheKey}: ${(e as Error).message}`, { cause: e })
    }
    const module = this.createModule(cacheKey, parent)
    module.exports = value
    module.loaded = true
    return module.exports
  }

  public loadBuiltin(request: string): unknown {
    if (request === 'process' || request === 'node:process') {
      return (this.runtime.context as any).process
    }
    if (request === 'console' || request === 'node:console') {
      return getConsoleFacade(this.runtime)
    }
    return require(request)
  }

  /**

View on GitHub (pinned to 50e974d969)

Solutions

  1. Validate the file: run `node -e "JSON.parse(require('fs').readFileSync('<path>','utf8'))"` and fix the syntax error reported at the given position
  2. Regenerate the JSON module file (reinstall or re-export it with JSON.stringify instead of hand-editing)
  3. Check the `cause` property of the thrown error for the exact line/column of the parse failure
  4. Ensure the file is UTF-8 encoded and complete (compare file size/checksum with a known-good copy)

Example fix

// before (hand-edited package-like module with trailing comma)
{ "name": "my-ext", "version": "1.0.0", }
// after
{ "name": "my-ext", "version": "1.0.0" }
Defensive patterns

Strategy: validation

Validate before calling

const src = fs.readFileSync(p, 'utf8').replace(/^\uFEFF/, '')
try { JSON.parse(src) } catch (e) { throw new Error(`Invalid JSON module ${p}: ${e.message}`) }

Try / catch

try {
  await loader.load(path)
} catch (e) {
  if (e.message.startsWith('Error parsing JSON module')) {
    logger.error(`Bad JSON at ${path}:`, e.cause ?? e)
  } else throw e
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: JSON parse error: "Unexpected token" / "not valid JSON" / "failed to parse" — what JSON parsers are really complaining about — this error's family across 45 libraries.

Related errors


AI-assisted analysis of neoclide/coc.nvim@50e974d969 (2026-08-31). Data as JSON: /api/errors/22856cdebbdb8f7b. Report an issue: GitHub.