Nuxt.js: Can't use process.env.SOME_VAR client-side without redefining it in nuxt.config.js

Created on 6 Nov 2019  路  4Comments  路  Source: nuxt/nuxt.js

What problem does this feature solve?

I'm using environment variables to configure my web app for different environments. Server-side, everything works fine, but client-side, using process.env.SOME_VAR results in undefined.

A workaround is to redefine all environment variable names in nuxt.config.js like so:

...
env: {
  VAR1: process.env.VAR1,
  VAR2: process.env.VAR2,
  VAR3: process.env.VAR3,
},
...

I don't see why it should be necessary to do this. It only results in needless duplication. The only use for this is setting defaults, but I don't understand why the environment variables aren't always available client-side.

What does the proposed changes look like?

All the environment variables should be available client-side without explicitly defining them in nuxt.config.js, though it should be possible to override them through nuxt.config.js for both client- and server-side.

This feature request is available on Nuxt community (#c9992)
feature-request

Most helpful comment

Environment variables often hold secrets. Exposing all environment variables to the client would be a security issue.

All 4 comments

Environment variables often hold secrets. Exposing all environment variables to the client would be a security issue.

@rchl fair point. I don't store any secrets myself. Would it be an idea to allow a less verbose option, where the env would be an array of variable names that should be mapped to the client directly?

env: ['VAR1', 'VAR2', 'VAR3'],

@WouterFlorijn Also see nuxt-env.

I think it would be easy enough to create code to do this for yourself:

env: ['VAR1', 'VAR2', 'VAR3'].reduce((env, v) => {
  env[v] = `process.env.${v}`
  return env
}, {}),
Was this page helpful?
0 / 5 - 0 ratings

Related issues

danieloprado picture danieloprado  路  3Comments

o-alexandrov picture o-alexandrov  路  3Comments

vadimsg picture vadimsg  路  3Comments

vadimsg picture vadimsg  路  3Comments

pehbehbeh picture pehbehbeh  路  3Comments