Gatsby - Build website for stage/development - which command should I use?

Viewed 1111

How can I build/deploy a gatsby website for development environment?

If you run gatsby build the environment variable is automatic set to production.

Use Case: I do want to deploy my website to services such as Netlify, etc, but I want to BUILD for STAGE environment. Anyone know which command I can use for that or if I should replace the NODE_ENV in the gatsby build command? I want to deploy and deliver the staging website to the coworkers/users/customers, then they will be able to test and use it on the staging environment. (with all staging configuration). If you are developing a website for customers you might need to deploy a staging version for test/validation purposes.

I know that gatsby/react internally have some different processing based on the environment, but I was wondering if it's possible to build and deploy a gatsby website for development/stage purposes using development variables configuration.

An example: I was using two plugins - gatsby-google-tag-manager and gatsby-plugin-facebook-pixel and realized that both configurations relies on NODE_ENV development or production and I currently can not build and deploy a website using development environment.

Any thoughts on that?

1 Answers

gatsby build overrides NODE_ENV environment variable.

I solved it by using another environment variable to override NODE_ENV in gatsby-config.js

At the top of the filegatsby-config.js:

if (process.env.DEVELOPMENT === 'TRUE') {
   process.env.NODE_ENV = 'development'
}

require ('dotenv'). config ({
   path: `.env. $ {process.env.NODE_ENV}`,
})

// etc...

Run development build:

DEVELOPMENT=TRUE gatsby build

Related