3

I have the following nuxt.config.js, where in the srcDir is pointing to "main-app" and I have placed the .env outside of it. So in the nuxt.config.js, how can set the custom path in line 1

require('dotenv').config({ path: '../.env' })

such that my process.env works enter image description here

Also the buildModules in nuxt.config.js is as follows

buildModules: ["@nuxtjs/fontawesome", "@nuxtjs/dotenv"],
kissu
  • 40,416
  • 14
  • 65
  • 133
Amaarockz
  • 4,348
  • 2
  • 9
  • 27

3 Answers3

4

If you are using version > 2.13 then you won't need to install dotenv anymore because it's already built in https://nuxtjs.org/docs/directory-structure/nuxt-config/#runtimeconfig

.env support Similar to vue-cli (*), .env file will be always loaded via dotenv and is accessible via process.env and options._env. process.env is updated so one can use it right inside nuxt.config for runtime config. Values are interpolated and expanded with an improved version of dotenv-expand. .env file is also watched to reload during nuxt dev. Path can be set via cli --dotenv or disabled by --dotenv false.

I created the .env.xxx files and created the corresponding scripts

file .env

file package.json

3

Actually I need to set the live path of the env in the buildModules section just like follows

 buildModules: ["@nuxtjs/fontawesome", ['@nuxtjs/dotenv', { path: './' }]],
Amaarockz
  • 4,348
  • 2
  • 9
  • 27
1

Did you tried this?

const { resolve } = require('path')
require('dotenv').config({ path: resolve(__dirname,"../.env") })

Taken from this answer.


Otherwise, it looks like there are a lot of solution on this question too: https://stackoverflow.com/a/42335383/8816585


EDIT: you could write this at the top of your nuxt.config.js file, outside of the export default's scope.

const { resolve } = require('path')
const current = resolve(__dirname)
const upper = resolve(__dirname, '..')
console.log('current', current)
console.log('upper', upper)

const testFolder = '../'

fs.readdir(testFolder, (_err, files) => {
  files.forEach((file) => {
    console.log(file)
  })
})

export default {
  publicRuntimeConfig: {
  // rest of the nuxt.config.js file below

Giving this on my Linux machine. enter image description here

kissu
  • 40,416
  • 14
  • 65
  • 133
  • I used require('dotenv').config({path: resolve(__dirname,"../.env")}) – Amaarockz Jun 15 '21 at 12:41
  • You should console.log the directory above to see if you do find the `.env` in it, and of course double-check that this is fine so far (that you're not one level above/deeper). – kissu Jun 15 '21 at 12:48
  • Actually putting console.log in nuxt.config.js is not showing up – Amaarockz Jun 15 '21 at 12:57
  • any other way to check? – Amaarockz Jun 15 '21 at 12:58
  • It should work. Google a way to find out how to print the content of the `__dirname` directory. Maybe this is a windows only issue? – kissu Jun 15 '21 at 12:59
  • mine is also linux only..let me check the edited answer – Amaarockz Jun 15 '21 at 13:29
  • I am trying to see the console.log in the browser...is that wrong? – Amaarockz Jun 15 '21 at 13:34
  • Should I seperately run the nuxt.config.js file then? – Amaarockz Jun 15 '21 at 13:34
  • This is backend code that you can only run on a `Node.js` environment. So, you need to write it on the "server". Edited my question again. But it's pretty much in the `nuxt.config.js` file, before the actual configuration. Or you can also make it in any `.js` file and call `node myFile.js` on it. – kissu Jun 15 '21 at 13:36
  • finally I was able to figure out Actually I was not setting the live path of the env file in the build modules The right path is defined as buildModules: ["@nuxtjs/fontawesome", ['@nuxtjs/dotenv', { path: './' }]], – Amaarockz Jun 15 '21 at 13:52