{"record":{"id":"9971fceaa7fe5670","repo":"gatsbyjs/gatsby","slug":"gatsby-command-can-only-be-run-for-a-gatsby-s","errorCode":null,"errorMessage":"gatsby <${command}> can only be run for a gatsby site.\nEither the current working directory does not contain a valid package.json or 'gatsby' is not specified as a dependency","messagePattern":"gatsby <(.+?)> can only be run for a gatsby site\\.\nEither the current working directory does not contain a valid package\\.json or 'gatsby' is not specified as a dependency","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"packages/gatsby-cli/src/create-cli.ts","lineNumber":66,"sourceCode":"  }\n\n  function getLocalGatsbyMajorVersion(): number | undefined {\n    const version = getLocalGatsbyVersion()\n\n    if (version) {\n      return Number(version.split(`.`)[0])\n    }\n\n    return undefined\n  }\n\n  function resolveLocalCommand(\n    command: string\n  ): ((...args: Array<unknown>) => void) | never {\n    if (!isLocalSite) {\n      cli.showHelp()\n      report.verbose(`current directory: ${directory}`)\n      return report.panic(\n        `gatsby <${command}> can only be run for a gatsby site.\\n` +\n          `Either the current working directory does not contain a valid package.json or ` +\n          `'gatsby' is not specified as a dependency`\n      )\n    }\n\n    try {\n      const cmdPath =\n        resolveCwd.silent(`gatsby/dist/commands/${command}`) ||\n        // Old location of commands\n        resolveCwd.silent(`gatsby/dist/utils/${command}`)\n      if (!cmdPath)\n        return report.panic(\n          `There was a problem loading the local ${command} command. Gatsby may not be installed in your site's \"node_modules\" directory. Perhaps you need to run \"npm install\"? You might need to delete your \"package-lock.json\" as well.`\n        )\n\n      report.verbose(`loading local command from: ${cmdPath}`)\n","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/gatsbyjs/gatsby/blob/8b06340921ffdf23125a365b9c9923690cb62ce6/packages/gatsby-cli/src/create-cli.ts#L48-L84","documentation":"Thrown by `resolveLocalCommand` when `isLocalSite` is false. `isLocalGatsbySite()` requires `./package.json` and looks for a `gatsby` entry in `dependencies` or `devDependencies`; if either is missing the directory is treated as not a Gatsby site. Most gatsby subcommands (develop, build, serve) are local-only and refuse to run otherwise.","triggerScenarios":"Running `gatsby <command>` (e.g. `gatsby develop`) in a directory whose `package.json` has no `gatsby` dependency, or in a directory with no `package.json` at all. The CLI shows help and panics.","commonSituations":"Wrong working directory (running from a parent folder or `~`); fresh checkout before `npm install` added gatsby; monorepo root where the site lives in a subdirectory; typo'd package name in `package.json` (e.g. `Gatsby`).","solutions":["`cd` into the directory containing the project `package.json`.","Confirm `package.json` lists `gatsby` under `dependencies` or `devDependencies` (case-sensitive).","If missing, install gatsby: `npm install gatsby` (or `yarn add gatsby`).","For monorepos, run the command from the package directory, not the workspace root."],"exampleFix":"// before: package.json missing gatsby\n{\n  \"name\": \"my-site\",\n  \"dependencies\": {}\n}\n\n// after\n{\n  \"name\": \"my-site\",\n  \"dependencies\": { \"gatsby\": \"^5.0.0\" }\n}","handlingStrategy":"validation","validationCode":"// Pre-flight check mirroring isLocalGatsbySite\nconst fs = require('fs'); const path = require('path')\nfunction isGatsbySite(dir = process.cwd()): boolean {\n  try {\n    const pkg = require(path.join(dir, 'package.json'))\n    return Boolean((pkg.dependencies && pkg.dependencies.gatsby) || (pkg.devDependencies && pkg.devDependencies.gatsby))\n  } catch { return false }\n}\nif (!isGatsbySite()) throw new Error('Run inside a Gatsby site directory')","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always run gatsby commands from the directory holding the project `package.json`.","Keep `gatsby` listed in `dependencies` (case-sensitive).","In monorepos, cd into the site package before running gatsby."],"tags":["cli","bootstrap","package-json","local-site"],"backgroundTag":null,"analyzedSha":"8b06340921ffdf23125a365b9c9923690cb62ce6","analyzedAt":"2026-08-13T02:36:21.405Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}