next.js: My .env variables just don't work!
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!
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 16 (6 by maintainers)
Commits related to this issue
- ❎ SAVE: Try as given the issue https://github.com/zeit/next.js/issues/7320 — committed to kumarabhirup/paprink by kumarabhirup 8 years ago
- ❎ SAVE: Do as stated in the given issue https://github.com/zeit/next.js/issues/7320 Everything breaks after making this change to the code. — committed to kumarabhirup/paprink by kumarabhirup 8 years ago
- 🐛 FIX: Make .env work! (#36) * ❎ SAVE: Do as stated in the given issue https://github.com/zeit/next.js/issues/7320 Everything breaks after making this change to the code. * 👌 IMPROVE: Update n... — committed to kumarabhirup/paprink by kumarabhirup 8 years ago
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.
what if you wanna have a different value in .env.development and another in .env.production ?
Thanks, @ijjk! I looked at docs and wondered if it could help me. But I am not gitignoring
next.config.jsto put env variables in there. And if I do, how do I do what’s written docs on current configuration of mynext.config.js?Hi @ijjk, hitting back here again. The same problem, but a different repo. Now when I try your solution,
envdoesn’t work. I am implementing something the wrong way probably 😅https://github.com/iqubex-technologies/adify/tree/d5a92640f1b45df3afb6e03125778ef320a84b1d
Access the
Next.jswebsite in thewebdirectory.You are god ❤️ @ijjk, it’s working after the update. Just that I need to check if things are working on production or not. Wish me luck.
Everything broke is just my way of expressing that I am sad 😅 Sorry if that sounds rude. 15 yo kid learning here.
I know this is a while afterwards but this should be the accepted solution. I just ran into this same problem but this fixed it with flying colors! Thanks!
Are you sure, that if I update Nextjs, things won’t break? Is it backward-compatible? What do I need to do to upgrade Nextjs? Does updating
nextnpm package do the trick? Please help.The code I am using doesn’t load the env in production on platforms like Now and Heroku.