问题
I have deployed my app on Heroku and on the start of my app I check a config file and in there I want to access a config var I have created on Heroku API_KEY
to define my Firebase config:
module.exports = {
fireConfig: {
apiKey: process.env.API_KEY,
authDomain: "my-app.firebaseapp.com",
databaseURL: "https://my-app.firebaseio.com",
projectId: "my-project-id",
storageBucket: "my-app.appspot.com",
messagingSenderId: "my-messaging-sender-id"
}
};
this process.env.API_KEY
is undefined
. Should I use a different way to access it?
回答1:
You can define environment variables in your nuxt.config.js file, e.g.
export default {
env: {
firebaseApiKey: process.env.API_KEY || 'default value'
}
}
Here we use the API_KEY
environment variable, if it's available, and assign that to firebaseApiKey
.
If that environment variable isn't set, e.g. maybe in development (you can set it there too if you want), we fall back to 'default value'
. Please don't put your real API key in 'default value'
. You could use a separate throwaway Firebase account key here or just omit it (take || 'default value'
right out) and rely on the environment variable being set.
These will be processed at build time and then made available using the name you give them, e.g.
module.exports = {
fireConfig: {
apiKey: process.env.firebaseApiKey, # Not API_KEY
// ...
};
来源:https://stackoverflow.com/questions/55704031/how-to-access-heroku-environment-variables-with-nuxt-js-app