Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.env vars in the front-end are not working #762

Closed
c3ho opened this issue Mar 1, 2020 · 5 comments
Closed

.env vars in the front-end are not working #762

c3ho opened this issue Mar 1, 2020 · 5 comments
Assignees
Labels
type: bug Something isn't working

Comments

@c3ho
Copy link
Contributor

c3ho commented Mar 1, 2020

What happened:
We can't use any of the .env vars in the front-end code. We have the following code in gatsby-config.js which should enable us to do so though

require('dotenv').config({
  path: path.join(__dirname, '../../', '.env'),
});

What should have happened:
Be able to use .env vars in front-end code
How to reproduce it (as precise as possible):

Anything else we need to know?:

Environment:

  • OS: Windows 10
  • Browser: Chrome
@c3ho c3ho added the type: bug Something isn't working label Mar 1, 2020
@humphd
Copy link
Contributor

humphd commented Mar 1, 2020

You have to expose these via sitemetadata, and I already have hooks and class-based ways to get it via Gatsby's GraphQL. See my changes in https://github.com/Seneca-CDOT/telescope/pull/690/files for an example. Just read in env vars at build time, like I do with telescopeUrl, and then expose them in the sitemetadata. Then you can get them in your components in the frontend.

@c3ho
Copy link
Contributor Author

c3ho commented Mar 1, 2020

I was checking your code, your comment at the top file made it seem like it was only for components would it work in the gatsby-config file?

@humphd
Copy link
Contributor

humphd commented Mar 1, 2020

The gatsby-config file can read env vars at build time, yes. That's how I do the telescopeUrl.

@c3ho c3ho assigned humphd and c3ho Mar 2, 2020
@c3ho
Copy link
Contributor Author

c3ho commented Mar 3, 2020

Solved

@c3ho c3ho closed this as completed Mar 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants