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

Environment Variables in gatsby-config.js not working #3896

Closed
pgegenfurtner opened this issue Feb 7, 2018 · 12 comments
Closed

Environment Variables in gatsby-config.js not working #3896

pgegenfurtner opened this issue Feb 7, 2018 · 12 comments

Comments

@pgegenfurtner
Copy link
Contributor

Description

I want to use environment variables in my frontend code and in my gatsby-config.js. I followed the steps from https://www.gatsbyjs.org/docs/environment-variables/ and for the frontend code it works perfectly with my two files: .env.development and .env.production.

Environment

Gatsby version: 1.9.183
Node.js version: 8.9.4
Operating System: macOS 10.13.3

File contents (if changed):

gatsby-config.js:

require('dotenv').config();
module.exports = {
  plugins: [
    {...}
    {
      resolve: `gatsby-plugin-google-analytics`,
      options: {
        trackingId: process.env.GA_KEY,
        anonymize: true,
      },
    },
  ],
};

package.json: not changed
gatsby-node.js: not changed
gatsby-browser.js: not changed
gatsby-ssr.js: not changed

Actual result

process.env.GA_KEY is undefiend

Expected behavior

process.env.GA_KEY is the value from my .env file

Am I doing something wrong here? I've read the documentation (gatsby and dotenv) multiple times and can't find the part which is wrong.

Thanks!

@KyleAMathews
Copy link
Contributor

Hmm not sure actually :-(

@m-allanson
Copy link
Contributor

@pgegenfurtner Do you have an example repo you could share?

@pgegenfurtner
Copy link
Contributor Author

I looked a bit into it. The problem is, that dotenv looks for a file named .env. I can specify a different path / name with a config object. require('dotenv').config({path:'.env.development'});. This works, but it won't load the correct file depending on dev or prod.

So you could do this

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

But buts works only in the prod environment (npm run build). NODE_ENV is not available to the development build (Todo @KyleAMathews ?).
You can go around it with setting the environment in develop command with NODE_ENV=development gatsby develop.

Now it works as I expect, but I see it only as a workaround (At least the modified develop command)

@m-allanson
Copy link
Contributor

m-allanson commented Feb 8, 2018

It sounds like a fix for this could be to modify gatsby develop so that it defaults to process.env.NODE_ENV === development?

I'm not sure what's best practise here.. Whether NODE_ENV=development gatsby develop is the correct approach or if Gatsby should try and be helpful by setting that for you (but allowing it to be overridden).

I'm happy to make a PR for this if @KyleAMathews approves 😀. It looks like a fairly straightforward change in gatsby-cli?

@KyleAMathews
Copy link
Contributor

@m-allanson ah yeah — we should do that 😄 we do force set NODE_ENV to production during builds so we should do the same for development.

@KyleAMathews
Copy link
Contributor

Well perhaps not force it during development. Just set it unless the user set something else. We have to have production during builds for a number of tools to work correctly.

@m-allanson
Copy link
Contributor

@pgegenfurtner With the latest version of gatsby-cli (1.1.39), NODE_ENV should default to develop during gatsby develop.

@pgegenfurtner
Copy link
Contributor Author

Nice.
I will make a PR with an updated version of the environment variables documentation, so that the implementation of dotenv is clear.

@pgegenfurtner
Copy link
Contributor Author

Okay, I think this can be closed then

@Redmega
Copy link

Redmega commented Feb 9, 2018

Should note that dot-env-cli is great for adding dot-env to run before an npm command. This is how I use .env config files with gatsby develop or build on my local.

"build": "dot-env gatsby develop"

@JimLynchCodes
Copy link
Contributor

I had to change the filename from .env to .env.development and prefix the variables with "GATSBY_"

@azmolmiah
Copy link

I just did the following...

require("dotenv").config({
  path: `.env`,
})

MrT3313 added a commit to MrT3313/NoteCards that referenced this issue Dec 10, 2019
gatsbyjs/gatsby#3896
process.env was returning undefined
added a config object into require(dotenv) w/ path: env.process.env.NODE_ENV
MrT3313 added a commit to MrT3313/NoteCards that referenced this issue Dec 11, 2019
* bug: process.env

gatsbyjs/gatsby#3896
process.env was returning undefined
added a config object into require(dotenv) w/ path: env.process.env.NODE_ENV

* updated: module.css => module.scss

<HomePage /> & <StudySetOption />

* updated: module.css => module.scss

rest of components using scss modules

stage: structure for new <LandingPage /> created and being renederd in src > pages > index.js

* updated: <LandingPage /> w/ scss

added: Google fonts
1. Bold = Permanent Market
2. Medium = Mansalva
3. Light = Indie Flower

* cleaned: removed all build warnings

* SCSS: overall app styling update & debugging boarders removed

* touchUps: variables for border-radius & transform rotation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants