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

Accessing gatsby env variables outside of gatsby - netlify-lambda #10813

Closed
shansmith01 opened this issue Jan 3, 2019 · 4 comments
Closed

Accessing gatsby env variables outside of gatsby - netlify-lambda #10813

shansmith01 opened this issue Jan 3, 2019 · 4 comments
Labels
stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby

Comments

@shansmith01
Copy link
Contributor

I am trying to reach my gatsby env variables outside of gatsby. Use case is for running functions in a local environment on netlify-lambda

So this looks like

  1. I have and env.production and env.development
  2. I have a src/lambda-src folder for keeping my functions. eg purchase.js
  3. I want to access the following variable in mu purchase.js
    const stripe = require('stripe')(process.env.GATSBY_STRIPE_SECRET_KEY);
  4. When building my netlify-lamda function is should see the right variable

This currently works on production on netlify, but i can get it to work locally. Any ideas on how i can expose my variables locally?

Environment (if relevant)



  System:
    OS: macOS 10.14.1
    CPU: (4) x64 Intel(R) Core(TM) i7-4558U CPU @ 2.80GHz
    Shell: 3.2.57 - /bin/bash
  Binaries:
    Node: 8.12.0 - /usr/local/bin/node
    Yarn: 1.12.3 - /usr/local/bin/yarn
    npm: 6.4.1 - /usr/local/bin/npm
  Languages:
    Python: 2.7.10 - /usr/bin/python
  Browsers:
    Chrome: 71.0.3578.98
    Firefox: 63.0.3
    Safari: 12.0.1
  npmPackages:
    gatsby: ^2.0.81 => 2.0.81 
    gatsby-image: ^2.0.25 => 2.0.25 
    gatsby-plugin-canonical-urls: ^2.0.8 => 2.0.8 
    gatsby-plugin-eslint: ^1.0.3 => 1.0.3 
    gatsby-plugin-google-tagmanager: ^2.0.7 => 2.0.7 
    gatsby-plugin-manifest: ^2.0.13 => 2.0.13 
    gatsby-plugin-netlify: ^2.0.6 => 2.0.6 
    gatsby-plugin-offline: ^2.0.20 => 2.0.20 
    gatsby-plugin-react-helmet: ^3.0.5 => 3.0.5 
    gatsby-plugin-react-svg: ^2.0.0 => 2.0.0 
    gatsby-plugin-remove-serviceworker: ^1.0.0 => 1.0.0 
    gatsby-plugin-remove-trailing-slashes: ^2.0.6 => 2.0.6 
    gatsby-plugin-robots-txt: ^1.3.0 => 1.3.0 
    gatsby-plugin-sentry: ^0.1.0 => 0.1.0 
    gatsby-plugin-sharp: ^2.0.17 => 2.0.17 
    gatsby-plugin-sitemap: ^2.0.4 => 2.0.4 
    gatsby-plugin-stripe-checkout: ^1.1.2 => 1.1.2 
    gatsby-plugin-stripe-elements: ^1.0.9 => 1.0.9 
    gatsby-plugin-styled-components: ^3.0.4 => 3.0.4 
    gatsby-plugin-subfont: ^1.0.3 => 1.0.3 
    gatsby-plugin-twitter: ^2.0.8 => 2.0.8 
    gatsby-plugin-webpack-bundle-analyzer: ^1.0.3 => 1.0.3 
    gatsby-source-datocms: ^2.0.1 => 2.0.1 
    gatsby-source-filesystem: ^2.0.12 => 2.0.12 
    gatsby-transformer-react-docgen: ^2.1.3 => 2.1.3 
    gatsby-transformer-remark: ^2.1.19 => 2.1.19 
    gatsby-transformer-sharp: ^2.1.10 => 2.1.10 
  npmGlobalPackages:
    gatsby-cli: 2.4.8

-->

File contents (if changed)

gatsby-config.js: N/A

const {
  NODE_ENV,
  URL: NETLIFY_SITE_URL = 'https://www.XXXXXXXXX.com',
  DEPLOY_PRIME_URL: NETLIFY_DEPLOY_URL = NETLIFY_SITE_URL,
  CONTEXT: NETLIFY_ENV = NODE_ENV
} = process.env;

const isNetlifyProduction = NETLIFY_ENV === 'production';
const siteUrl = isNetlifyProduction ? NETLIFY_SITE_URL : NETLIFY_DEPLOY_URL;

var proxy = require('http-proxy-middleware');

module.exports = {
  developMiddleware: app => {
    app.use(
      '/.netlify/functions/',
      proxy({
        target: 'http://localhost:9000',
        pathRewrite: {
          '/.netlify/functions/': ''
        }
      })
    );
  },
  siteMetadata: {
    title: 'XXXXXXXXX XXXXXXXXX',
    siteUrl: 'https://www.XXXXXXXXX.com'
  },
  plugins: [
    'gatsby-plugin-react-helmet',
    `gatsby-transformer-sharp`,
    `gatsby-plugin-sharp`,
    `gatsby-plugin-styled-components`,
    `gatsby-transformer-remark`,
    `gatsby-plugin-stripe-elements`,
    `gatsby-transformer-react-docgen`,
    {
      resolve: 'gatsby-plugin-webpack-bundle-analyzer',
      options: {
        analyzerPort: 3000,
        production: false
      }
    },
    {
      resolve: `gatsby-plugin-sitemap`,
      options: {
        exclude: [
          `/about/privacy-policy/`,
          '/about/cookie-policy/',
          '/about/terms-and-conditions/',
          '/docs/',
          '/docs/*'
        ]
      }
    },

    {
      resolve: `gatsby-plugin-manifest`,
      options: {
        name: `XXXXXXXXX XXXXXXXXX`,
        short_name: `XXXXXXXXX`,
        start_url: `/?utm_source=homescreen`,
        background_color: `#f7f0eb`,
        theme_color: `#b60300`,
        display: `standalone`,
        icon: `src/images/logo.png` // This path is relative to the root of the site.
      }
    },
    {
      resolve: `gatsby-plugin-canonical-urls`,
      options: {
        siteUrl: `https://www.XXXXXXXXX.com`
      }
    },
    {
      resolve: 'gatsby-plugin-robots-txt',
      options: {
        resolveEnv: () => NETLIFY_ENV,
        env: {
          production: {
            sitemap: 'https://www.XXXXXXXXX.com/sitemap.xml',
            policy: [{ userAgent: '*' }]
          },
          'branch-deploy': {
            policy: [{ userAgent: '*', disallow: ['/'] }],
            sitemap: null,
            host: null
          },
          'deploy-preview': {
            policy: [{ userAgent: '*', disallow: ['/'] }],
            sitemap: null,
            host: null
          }
        }
      }
    },
    {
      resolve: `gatsby-source-datocms`,
      options: {
        apiToken: `2f10bdfdabc47fc2f76f98ef230ceb`,
        previewMode: false,
        disableLiveReload: true
      }
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `images`,
        path: `${__dirname}/src/images/`
      }
    },
    {
      resolve: `gatsby-source-filesystem`,
      options: {
        name: `components`,
        path: `${__dirname}/src/components/`
      }
    },

    {
      resolve: 'gatsby-plugin-sentry',
      options: {
        dsn: 'XXXXXXXX',
        // Optional settings, see https://docs.sentry.io/clients/node/config/#optional-settings
        environment: process.env.NODE_ENV,
        enabled: (() =>
          ['production', 'stage'].indexOf(process.env.NODE_ENV) !== -1)()
      }
    },
    `gatsby-plugin-twitter`,
    {
      resolve: `gatsby-plugin-google-tagmanager`,
      options: {
        id: '	XXXXXXX',

        // Include GTM in development.
        // Defaults to false meaning GTM will only be loaded in production.
        includeInDevelopment: true

        // Specify optional GTM environment details.
        // gtmAuth: "YOUR_GOOGLE_TAGMANAGER_ENVIROMENT_AUTH_STRING",
        // gtmPreview: "YOUR_GOOGLE_TAGMANAGER_ENVIROMENT_PREVIEW_NAME",
      }
    },

    `gatsby-plugin-offline`,
    `gatsby-plugin-netlify`
  ]
};

package.json: N/A


{
  "name": "gatsby-starter-default",
  "description": "Gatsby default starter",
  "version": "1.0.0",
  "author": "Kyle Mathews <mathews.kyle@gmail.com>",
  "dependencies": {
    "@bit/shansmith01.common.common.button": "file:./src/components/common/Button",
    "@bit/shansmith01.common.common.container": "file:./src/components/common/Container",
    "@bit/shansmith01.common.common.scroll-container": "file:./src/components/common/ScrollContainer",
    "@bit/shansmith01.common.common.section": "file:./src/components/common/Section",
    "@sentry/browser": "^4.4.2",
    "ajv": "^6.6.2",
    "babel-plugin-styled-components": "^1.10.0",
    "detect-browser": "^3.0.1",
    "dotenv": "^6.2.0",
    "gatsby": "^2.0.81",
    "gatsby-image": "^2.0.25",
    "gatsby-plugin-canonical-urls": "^2.0.8",
    "gatsby-plugin-google-tagmanager": "^2.0.7",
    "gatsby-plugin-manifest": "^2.0.13",
    "gatsby-plugin-netlify": "^2.0.6",
    "gatsby-plugin-offline": "^2.0.20",
    "gatsby-plugin-react-helmet": "^3.0.5",
    "gatsby-plugin-react-svg": "^2.0.0",
    "gatsby-plugin-remove-serviceworker": "^1.0.0",
    "gatsby-plugin-remove-trailing-slashes": "^2.0.6",
    "gatsby-plugin-robots-txt": "^1.3.0",
    "gatsby-plugin-sentry": "^0.1.0",
    "gatsby-plugin-sharp": "^2.0.17",
    "gatsby-plugin-sitemap": "^2.0.4",
    "gatsby-plugin-stripe-checkout": "^1.1.2",
    "gatsby-plugin-stripe-elements": "^1.0.9",
    "gatsby-plugin-styled-components": "^3.0.4",
    "gatsby-plugin-subfont": "^1.0.3",
    "gatsby-plugin-twitter": "^2.0.8",
    "gatsby-plugin-webpack-bundle-analyzer": "^1.0.3",
    "gatsby-source-datocms": "^2.0.1",
    "gatsby-source-filesystem": "^2.0.12",
    "gatsby-transformer-react-docgen": "^2.1.3",
    "gatsby-transformer-remark": "^2.1.19",
    "gatsby-transformer-sharp": "^2.1.10",
    "google-maps-react": "^2.0.2",
    "netlify-lambda": "^1.1.1",
    "react": "^16.7.0",
    "react-dom": "^16.7.0",
    "react-helmet": "^5.2.0",
    "react-images": "^0.5.19",
    "react-oembed-container": "^0.3.0",
    "react-player": "^1.8.0",
    "react-scrollspy": "^3.3.5",
    "react-share": "^2.4.0",
    "react-stripe-elements": "^2.0.1",
    "stripe": "^6.20.0",
    "styled-components": "^4.1.3",
    "styled-components-breakpoint": "^2.1.1",
    "typeface-pt-sans": "0.0.54"
  },
  "keywords": [
    "gatsby"
  ],
  "license": "MIT",
  "scripts": {
    "build": "gatsby build && netlify-lambda build src/lambda-src",
    "build:lambda": "netlify-lambda build src/lambda-src",
    "develop": "gatsby develop",
    "develop:lambda": "netlify-lambda serve src/lambda-src",
    "format": "prettier --write '**/*.js'",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "devDependencies": {
    "babel-eslint": "^9.0.0",
    "eslint": "^5.11.1",
    "eslint-config-airbnb": "^17.1.0",
    "eslint-config-prettier": "^3.3.0",
    "eslint-loader": "^2.1.1",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-jsx-a11y": "^6.1.2",
    "eslint-plugin-prettier": "^2.7.0",
    "eslint-plugin-react": "^7.12.1",
    "gatsby-plugin-eslint": "^1.0.3",
    "http-proxy-middleware": "^0.19.1",
    "prettier": "1.14.2"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/gatsbyjs/gatsby-starter-default"
  },
  "browserslist": [
    "> 0.25%",
    "not dead",
    " not android 4.1",
    "not android 4.2",
    "not android 4.4",
    "not android 4.4.3",
    "not chrome 29",
    "not chrome 43",
    "not chrome 49",
    "not chrome 54",
    "not firefox 47",
    "not firefox 48",
    "not firefox 51",
    "not firefox 52",
    "not ios 8.1",
    "not ios 9.3",
    "not safari 5.1",
    "not safari 9.1"
  ]
}




@jgierer12 jgierer12 added the type: question or discussion Issue discussing or asking a question about Gatsby label Jan 3, 2019
@jgierer12
Copy link
Contributor

You can use the dotenv package to load environment variables from a .env file. Since your scripts are in a subdirectory, you may need to provide a custom path to your .env.

@gatsbot gatsbot bot added the stale? Issue that may be closed soon due to the original author not responding any more. label Feb 11, 2019
@gatsbot
Copy link

gatsbot bot commented Feb 11, 2019

Hiya!

This issue has gone quiet. Spooky quiet. 👻

We get a lot of issues, so we currently close issues after 30 days of inactivity. It’s been at least 20 days since the last update here.

If we missed this issue or if you want to keep it open, please reply here. You can also add the label "not stale" to keep this issue open!

Thanks for being a part of the Gatsby community! 💪💜

@gatsbot
Copy link

gatsbot bot commented Feb 22, 2019

Hey again!

It’s been 30 days since anything happened on this issue, so our friendly neighborhood robot (that’s me!) is going to close it.

Please keep in mind that I’m only a robot, so if I’ve closed this issue in error, I’m HUMAN_EMOTION_SORRY. Please feel free to reopen this issue or create a new one if you need anything else.

Thanks again for being part of the Gatsby community!

@gatsbot gatsbot bot closed this as completed Feb 22, 2019
@iamskok
Copy link

iamskok commented Jun 10, 2019

@shansmith01 were you able to find a solution? Do you mind posting it here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stale? Issue that may be closed soon due to the original author not responding any more. type: question or discussion Issue discussing or asking a question about Gatsby
Projects
None yet
Development

No branches or pull requests

3 participants