CuteMachine

Gatsby Executing Command

Today I learned how to get the Gatsby Executing Command in createPages().

I wanted to create pages with Gatsby programmatically. But I wanted to have these pages only for development, not in my production built of the site.

Therefore I needed a way to see in the createPage() function if I am in dev mode or build mode.

You can make this differentiation by checking the environment of the process. Add this to your gatsby-node.js file:


exports.createPages = async ({ graphql, actions }) => {

  console.log(`Executing command: ${process.env.gatsby_executing_command}`)

}

Now, when you run gatsby develop, you will see the following in your terminal:


Executing command: develop

When you run gatsby build, you will see this output instead:


Executing command: build

You can use this information to create pages during development only. For example, to make a screenshot programmatically.

Posted on CuteMachine in gatsby.

Jo's Profile ImageWritten by Jo who lives and works in Frankfurt building digital doodah. Stalk him on Twitter.

TOS • Privacy • Disclaimer