nuxt.config.js
modules: [
'@nuxtjs/dotenv'
],
server/index.js
const express = require('express')
const consola = require('consola')
const { Nuxt, Builder } = require('nuxt')
const app = express()
const host = process.env.HOST || '0.0.0.0'
const port = 8080
app.set('port', port)
// Import and Set Nuxt.js options
let config = require('../nuxt.config.js')
config.dev = !(process.env.NODE_ENV === 'production')
const Storage = require('@google-cloud/storage')
const dotenv = require('dotenv')
async function getEnv() {
if (config.dev) {
dotenv.config({ path: '.env' })
console.log('Environment local .env file loaded.')
console.log(process.env.LOCALE)
return
}
try {
const bucketName = 'env-var'
const dotEnvSourcePath = `.env`
const dotEnvDestinationPath = `/tmp/${dotEnvSourcePath}`
const storage = new Storage({})
await storage
.bucket(bucketName)
.file(dotEnvSourcePath)
.download({ destination: dotEnvDestinationPath })
console.log(
`gs://${bucketName}/${dotEnvSourcePath} downloaded to ${dotEnvDestinationPath}.`
)
dotenv.config({ path: dotEnvDestinationPath })
} catch (err) {
console.error('ERROR:', err)
}
}
async function afterEnvProcess() {
// Init Nuxt.js
const nuxt = new Nuxt(config)
// Build only in dev mode
if (config.dev) {
const builder = new Builder(nuxt)
await builder.build()
}
// Give nuxt middleware to express
app.use(nuxt.render)
// Listen the server
app.listen(port, host)
consola.ready({
message: `Server listening on http://${host}:${port}`,
badge: true
})
const fs = require('fs')
const dotEnvExists = fs.existsSync('.env')
}
getEnv()
.then(r => afterEnvProcess())
.catch(e => console.log(e))
I get the values for process.env.<variable>
as undefined
when running the app in production. When running in development, I get the values correctly. It seems the env variables are not getting passed to the nuxt env property.
EDIT 1: I can see the correct values in google cloud logs when I console log the env variables with process.env. but at the same time those console log statements show undefined in the browser console