Skip to content

Gatsby Exposing Environment Variables on "Gatsby Build" #38737

Answered by 808vita
jonolane asked this question in Help
Discussion options

You must be logged in to vote

Attaching links to gatsby docs , hope this helps.
https://www.gatsbyjs.com/docs/how-to/local-development/environment-variables/

.env.development
Copy.env.development: copy code to clipboard
GATSBY_API_URL=https://dev.example.com/api
API_KEY=927349872349798

To load these into Node.js, add the following code snippet to the top of your gatsby-config.js file:

gatsby-config.js
Copygatsby-config.js: copy code to clipboard
require("dotenv").config({
  path: `.env.${process.env.NODE_ENV}`,
})

This loads process.env.GATSBY_API_URL and process.env.API_KEY for use in gatsby-*.js files and functions.

For example, when configuring a plugin in gatsby-config.js:


gatsby-config.js
Copygatsby-config.js…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by jonolane
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants