简体   繁体   中英

Cannot access environment variables in the server/index.js in Nuxt.js

I'm trying to add environment variables to the nuxt.config.js file in the env property to access the variables in server/index.js file but it gives undefined . According to the env documentation, the variables should work in both client and sever. Not sure if i'm missing something? Thanks in advance.

module.exports = {
  env: {
    baseUrl: process.env.BASE_URL || 'http://localhost:3000'
  }
}

In your .env file rename variable like VUE_APP_BASE_URL , and use it with that name.

.env file just allow variables that starts with VUE_APP .

You can read more here .

All env variables in Nuxt.js must start with NUXT_ENV . In vue.js it must start with 'VUE_APP'

I know it's quite a late answer but just came across the same issue and thought my findings might help to some future mate in trouble.

It was as simple as include the require('dotenv').config() before any process.env.XXX reference.

Note that you might have better solution if using nuxt 2.13+.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM