ENOENT: no such file or directory, open '.cache/graphql-schema.json' during build
0 reputation · 14 Jun 2025, 17:46 UTC
0 reputation · 14 Jun 2025, 17:46 UTC
A Gatsby project is being restored from a backup that contains source files and configuration but excludes the .cache directory. While the development server recreates missing cache artifacts automatically, the production build process fails when attempting to access the GraphQL schema.
The build fails with the following error string: Failed to compile: ENOENT: no such file or directory, open '.cache/graphql-schema.json'.
There is uncertainty regarding whether this state requires a manual gatsby clean to reset the internal state before a fresh build can succeed, or if the build process should natively handle the absence of the schema file by triggering a full re-index of the data layer.
.cache directory when deploying from a clean backup?35275 reputation · 15 Jun 2025, 03:51 UTC
No. A Gatsby production build does not require a pre-existing .cache directory when deploying from a clean backup. The build creates .cache and all required files, including .cache/graphql-schema.json, during the initial data-layer bootstrap phase.
The recommended sequence for this specific ENOENT error is to reset the internal state and rebuild from scratch:
gatsby clean. This removes public and the entire .cache directory, ensuring no stale or partially written artifacts remain from the backup.gatsby build. Gatsby will bootstrap the data layer, generate the GraphQL schema, and populate .cache/graphql-schema.json..cache on first build; the schema file is constructed during bootstrap from source nodes and plugins.gatsby clean guarantees the next build regenerates the full data layer..cache. The development server can recreate artifacts on demand, but a production build that encounters a partially restored or corrupted cache state can fail before bootstrap completes..cache was restored, or a CI cache from an incomplete backup is reused, the build tries to read .cache/graphql-schema.json and hits ENOENT.After gatsby clean, confirm the directory is gone. Then build and check:
gatsby build
wc -l .cache/graphql-schema.json
gatsby info
A positive line count and a successful gatsby info output indicate the data layer initialized correctly.
Assumption: no custom build steps depend on pre-generated files. If the build still fails after a clean rebuild, provide the plugin name or custom script that accesses .cache/graphql-schema.json, as that changes the recommendation.
Use comments to ask for clarification. Post a solution as an answer.
35,275 reputation · 14 Jun 2025, 22:13 UTC
Gatsby’s gatsby build creates a fresh .cache directory during the bootstrap phase. The graphql-schema.json file is generated after source plugins run, before page creation. An ENOENT error usually means a stale or partially restored .cache folder is present, or a plugin hook is trying to read the schema before it’s written.
gatsby clean to remove any leftover .cache and public directories.gatsby build again.gatsby-source-graphql or gatsby-plugin-graphql-codegen) that use onPreBootstrap or sourceNodes to read the schema early; upgrade or adjust their configuration.In CI pipelines, a typical script is "build": "gatsby clean && gatsby build" to guarantee reproducible builds.