gatsbyjs/gatsby · error

json-file-store failed to JSON.parse this string: `${dataStr

Error message

json-file-store failed to JSON.parse this string: `${dataString.replace(/\n/g, `⏎`)}`

What it means

`json-file-store` persists Gatsby's cache as JSON and parses it back with a reviver. When `JSON.parse` throws, the raw string is shown with newlines visualised as `⏎` so corruption is diagnosable. Typical cause is a truncated/garbled cache file.

Source

Thrown at packages/gatsby/src/cache/json-file-store.ts:141

        // JSON.parse is sync so we need to return a buffer sync, we will fill the buffer later
        const buffer = Buffer.alloc(value.size)
        externalBuffers.push({
          index: +value.index,
          buffer: buffer,
        })
        return buffer
      } else if (
        value &&
        value.type === `Infinity` &&
        typeof value.sign === `number`
      ) {
        return Infinity * value.sign
      } else {
        return value
      }
    })
  } catch (e) {
    throw new Error(
      "json-file-store failed to JSON.parse this string: `" +
        dataString.replace(/\n/g, `⏎`)
    )
  }

  // read external buffers
  await Promise.all(
    externalBuffers.map(async function (externalBuffer) {
      if (options.zip) {
        const bufferCompressed = await promisify(fs.readFile)(
          path + `-` + +externalBuffer.index + `.bin` + zipExtension
        )
        const buffer = await promisify(zlib.unzip)(bufferCompressed)
        buffer.copy(externalBuffer.buffer)
      } else {
        const fd = await promisify(fs.open)(
          path + `-` + +externalBuffer.index + `.bin` + zipExtension,
          `r`

View on GitHub (pinned to 8b06340921)

Solutions

  1. Delete `.cache` (and `public` for a clean build) and re-run.
  2. Check disk space and that no other process is writing to `.cache`.
  3. Ensure builds shut down gracefully so writes can complete.
  4. If reproducible on a clean cache, capture the failing file and file a Gatsby issue.
Defensive patterns

Strategy: retry

Validate before calling

// Pre-flight: ensure cache files parse; if not, clear before build.
const fs = require("fs")
function ensureCacheValid(file) {
  try { JSON.parse(fs.readFileSync(file, "utf8")) } catch { fs.rmSync(".cache", { recursive: true, force: true }) }
}

Try / catch

// CI build wrapper: one cache-clear retry on parse failure.
try { await build() }
catch (e) {
  if (/json-file-store failed to JSON.parse/.test(String(e))) {
  fs.rmSync(".cache", { recursive: true, force: true })
  await build()
  } else throw e
}

Prevention

When it happens

Trigger: The cache file on disk contains bytes that are not valid JSON (truncated write, concurrent writers, disk-full, manual edit).

Common situations: Build killed mid-write (`SIGKILL`, OOM, power loss); out of disk; switching Node/Gatsby versions over the same `.cache`; editor saving over a cache file.

Related errors


AI-assisted analysis of gatsbyjs/gatsby@8b06340921 (2026-08-13). Data as JSON: /api/errors/ee074b1b78ab72ad. Report an issue: GitHub.