Gatsby build fails as Gatsby command on installed

After following the docs, my new Gatsby project build fails as the Gatsby command is not installed. I’ve marked the project as a Gatsby project in the build settings for Pages. is there anything else I have to do?

|10:12:19.440||
|---|---|
|10:12:19.440|found 0 vulnerabilities|
|10:12:19.473|Executing user command: gatsby build|
|10:12:19.480|/bin/sh: 1: gatsby: not found|
|10:12:19.482|Failed: Error while executing user command. Exited with error code: 127|
|10:12:19.491|Failed: build command exited with code: 1|
|10:12:20.335|Failed: error occurred while running build command|

Which build image version with Pages are you using?

Build image v1 added the local node_modules/.bin to PATH so this kinda thing worked, but was a little non-standard.

Build image v2 no longer does this, so the recommended approach is via an npm run script, or npx.

I’m not sure what the npm equivalent of Gatsby build is. All the Gatsby (and the cloudflare docs) talk about running Gatsby build.

In your package.json, you’ll want to add a scripts section like this, if you don’t already have one:

  "scripts": {
    "develop": "gatsby develop",
    "start": "gatsby develop",
    "build": "gatsby build",
    "serve": "gatsby serve",
    "clean": "gatsby clean"
  },

You can then set your build to npm run build. Or alternatively you can use npx with:
npx gatsby build.

in fact, when you select Gatsby in the framework dropdown, it pre-fills Gatsby build as the build command.

I wonder if the issue is that cloudflare is not detecting Gatsby (even though it’s in the package.json). If you look at a build log, it has Detected the following tools from environment: [email protected], [email protected]

ok, that worked. Can you explain why though. If Gatsby is not installed, how does npm run build work as it ends up calling the same Gatsby build as I typed from the command line.

either way, thanks for your help

Glad to hear it’s working now!

gatsby is installed, but in node_modules/.bin. npm is smart and will look into this folder for what to run.

Pages Build Image v1 did some hackery magic adding node_modules/.bin to your PATH so you could just run gatsby directly, but this was non-standard, so is no longer done in Pages Build Image v2.

1 Like

thanks. I’ve left feedback on the Gatsby page on the Pages docs with the details so hopefully they update their docs. Ideally they also update the default build command in the build settings as well. Anyone using Gatsby will run into this.

much appreciated