Reputation: 4684
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
Also the buildModules in nuxt.config.js is as follows
buildModules: ["@nuxtjs/fontawesome", "@nuxtjs/dotenv"],
Upvotes: 3
Views: 3261
Reputation: 263
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
Upvotes: 5
Reputation: 4684
Actually I need to set the live path of the env in the buildModules section just like follows
buildModules: ["@nuxtjs/fontawesome", ['@nuxtjs/dotenv', { path: './' }]],
Upvotes: 3
Reputation: 46696
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.
Upvotes: 1