4

I'm searching how to use a .env value in nuxt.config.js with runtime config.

Declare it, to use it in "normal" code is ok.

  publicRuntimeConfig: {
    URL_API: process.env.URL_API || 'http://localhost:8000/',
  },

But I want use .env value like this in my nuxt.config.js

  auth: {
    strategies: {
      local: {
        token: {
          property: 'token',
          required: true,
          maxAge: 1000 * 60 * 60
        },
        user: {
          property: 'user',
          autoFetch: false
        },
        clientID: true,
        endpoints: {
          login: { url: `${process.env.URL_API}/auth/login`, method: 'post' },
          logout: { url: `${process.env.URL_API}/auth/logout`, method: 'post' },
        },
        tokenType: ''
      }
    },
    redirect: {
      login: '/auth/login',
      logout: '/',
      callback: '/auth/login',
      home: '/'
    }
  },

Any idea?

kissu
  • 40,416
  • 14
  • 65
  • 133
Andy
  • 189
  • 2
  • 13

2 Answers2

2

As explained in my answer here: https://stackoverflow.com/a/67705541/8816585

If you have some modules located in nuxt.config.js, you can only pass env variables through process.env.MY_VARIABLE.
It is working if you're linking to an external file tho.

kissu
  • 40,416
  • 14
  • 65
  • 133
-2

You can use dotenv package to use variables from .env file in nuxt.config.js.

nuxt.config.js file should look like this:

// your imports here

require('dotenv').config()

// nuxt config here
  • In NuxtJS, there is runtime config, which allow us to not use dotenv. I'm searching to use .env from with runtime config, not dotenv package – Andy Sep 05 '21 at 10:24
  • Please do not use `dotenv` since it's already baked in. – kissu Sep 05 '21 at 10:27