question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

My .env variables just don't work!

See original GitHub issue

Bug report

every .env variable fails to show up even by using process.env.VARIABLE clause.

To Reproduce

  • next.config.js:
const withCSS = require('@zeit/next-css');
const withSass = require('@zeit/next-sass')
const { parsed: localEnv } = require('dotenv').config()
const webpack = require('webpack')

module.exports = withSass({
  webpack(config) {
    config.plugins.push(new webpack.EnvironmentPlugin(localEnv))
    config.plugins.push(
      new webpack.ProvidePlugin({
          '$': 'jquery',
          'jQuery': 'jquery',
      })
    )
    return config
  }
})
  • constants.js:
export const BACKEND_SERVER = process.env.BACKEND_SERVER
export const PROD_DOMAIN = process.env.PROD_DOMAIN
export const FB_LOGIN_APP_ID = process.env.FB_LOGIN_APP_ID
export const GOOGLE_LOGIN_APP_ID = process.env.GOOGLE_LOGIN_APP_ID
export const GOOGLE_LOGIN_APP_SECRET = process.env.GOOGLE_LOGIN_APP_SECRET
export const FB_LOGIN_APP_VERSION = process.env.FB_LOGIN_APP_VERSION
export const DEV_DOMAIN = process.env.DEV_DOMAIN
export const ENDPOINT = process.env.ENDPOINT
export const GA_TRACKING_ID = process.env.GA_TRACKING_ID
export const CLOUDINARY_USERNAME = process.env.CLOUDINARY_USERNAME
  • App.js
import { BACKEND_SERVER } from './constants'
export const client = new ApolloClient({
  uri: `${BACKEND_SERVER}/graphql`,
  request: operation => {
    operation.setContext({
      fetchOptions: {
        credentials: 'include',
      }
    })
  }
})

Expected behavior

The above example should make env variable work nicely, but just doesn’t!

System information

  • OS: macOS 18.0.0
  • Browser (if applies) Chrome Latest
  • Version of Next.js: v7.0.2

Additional context

Please help, trying to solve the bug since two days!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:16 (6 by maintainers)

github_iconTop GitHub Comments

78reactions
ijjkcommented, May 13, 2019

You should be able to upgrade to v8 from v7.0.2 to do so you do yarn add next@latest react@latest react-dom@latest

To have better compatibility with Now and Heroku I would use process.env to access the values. So in development you can use dotenv to load your values and then in production they will be added to the environment by configuring them on the platform you are using. Here are the docs for configuring them on Now.

So your next.config.js would look like this:

require('dotenv').config()
const withCSS = require('@zeit/next-css');
const withSass = require('@zeit/next-sass')
const webpack = require('webpack')

module.exports = withSass({
  webpack(config) {
    config.plugins.push(
      new webpack.ProvidePlugin({
          '$': 'jquery',
          'jQuery': 'jquery',
      })
    )
    return config
  },
  env: {
    'MY_SECRET_KEY': process.env.MY_SECRET_KEY
  }
})

This will allow you to use process.env.MY_SECRET_KEY in your built files. Note: if you change these values you will need to run a new build.

13reactions
ijjkcommented, May 13, 2019

Hi, have you tried using the built-in support for inlining values during build? Here are the docs in case you missed them.

Read more comments on GitHub >

github_iconTop Results From Across the Web

5 reasons why your .env environment variables don't work
5 reasons why your .env environment variables don't work · 1. Your framework doesn't automatically load .env files. · 2. You added or...
Read more >
Problems with Environment Variables
To check which environment variables are set, do the following (works for all versions of Windows): Open a DOS box (or a Command...
Read more >
Windows 10 System environment variables don't stick
I am unable to update my system environment variable PATH. I don't receive any error message when editing PATH, but the change doesn't...
Read more >
Why are my custom process.env not working within dotenv?
dotenv file is not loading environment variables. I'm trying to set an env file locally based on process.env.NODE_ENV . The application would be ......
Read more >
New Environment Variables Not Working in Windows 10
I have also tried it without the trailing slash, which doesn't work as well. I have also tried with the name of the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found