My .env variables just don't work!
See original GitHub issueBug 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:
- Created 4 years ago
- Comments:16 (6 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

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@latestTo have better compatibility with Now and Heroku I would use
process.envto access the values. So in development you can usedotenvto 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.jswould look like this:This will allow you to use
process.env.MY_SECRET_KEYin your built files. Note: if you change these values you will need to run a new build.Hi, have you tried using the built-in support for inlining values during build? Here are the docs in case you missed them.