Next.js: How to set env that can be accessed both by server and client?

Created on 14 Mar 2017  路  11Comments  路  Source: vercel/next.js

In Meteor, we can use the SETTINGS env.
In create-react-app app, we can set REACT_APP_XXX.

How to do similar things in Next.js?

Most helpful comment

All 11 comments

But I do think this raises a valid concern, that something easier ought to be baked-in. Why Next.js couldn't have a similar env var?

I have tried this in my app, but it complains:
image

project structure
image

.babelrc
image

env-config.js
image

usage in settings.js
image

you can have a try with this repo
https://github.com/zhaoyao91/docit/tree/show-global-env-problem

Lets re-open it then.

Here's how to fix this problem.

You need to have a value for "APP_SERVER_URL" always. So, do something like this:

module.exports = {
  APP_SERVER_URL: process.env.APP_SERVER_URL || 'http://some.url'
};

And make sure to clean the babel cache by:

  • Running rm -rf node_modules/.cache or
  • Changing the content inside the page.

@zhaoyao91 Reopen again, it this doesn't fix your app.

I'm also getting the same error @arunoda . This issue might stem from a recent commit/update.

@pauldariye did you follow my steps here: https://github.com/zeit/next.js/issues/1415#issuecomment-286489055

If that didn't work well, send me a sample repo.

Can cache cleaning be avoided?

This might be what you are looking for ?
https://github.com/zeit/next.js#exposing-configuration-to-the-server--client-side

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  路  3Comments

jesselee34 picture jesselee34  路  3Comments

lixiaoyan picture lixiaoyan  路  3Comments

swrdfish picture swrdfish  路  3Comments

formula349 picture formula349  路  3Comments