2

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'
  }
}
4
  • no, declaring env var in nuxt config means its accessible in nuxt code, not in external node code. Commented Jul 16, 2019 at 0:54
  • Oh okay! But then what does it mean in the documentation nuxtjs.org/api/configuration-env which says Nuxt.js lets you create environment variables that will be shared for the client and server-side. It mentions the server side. Commented Jul 16, 2019 at 2:40
  • 1
    You misunderstand what is server side. Server side isnt a backend. Its nuxt code that is executed on server side. Commented Jul 16, 2019 at 8:20
  • Got it and this makes things clear. Thanks!! Commented Jul 16, 2019 at 20:30

3 Answers 3

4

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

I think you misunderstood! It's not the issue with the BASE_URL variable itself but declared variables in env property of nuxt.config.js itself. The variable i declare should be accessible in client and server. Which if i understand should accessible in server/index.js (node entry file) but instead i get undefined.
0

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+.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.