sandrooco
sandrooco

Reputation: 8716

Vue.js/Template: Using .env?

I have a vue.js project based on the webpack template. We deploy to different cloudfoundry environments: Dev, Int and Prod. For Int and Prod I want to run the app in production mode but with different env variables. I tried to use the following based on a forum thread:

module.exports = {
    NODE_ENV: '"production"',
    SC_AUTH_CLIENT_ID: JSON.stringify(process.env.AUTH_CLIENT_ID),
    API_URL: JSON.stringify(process.env.API_BASE_URL),
};

Both SC_AUTH_CLIENT_ID and API_URL are undefined (although the variables are set in the cf config) - why?

Upvotes: 1

Views: 988

Answers (2)

Daniel Mikusa
Daniel Mikusa

Reputation: 15006

You should be able to use environment variables set through Cloud Foundry to accomplish what you want.

Ex:

  1. cf push your-app --no-start
  2. cf set-env your-app NODE_ENV 'development'
  3. cf start your-app

Then process.env should contain NODE_ENV set to development. By default, NODE_ENV should be set to production as the Node.js buildpack sets that automatically. You can set other variables in the exact same way.

You do not need a third party library to read them. Cloud Foundry should expose them as actual environment variables to your process which you can read using the standard method for reading environment variables in your language of choice (eg. process.env in Node.js).

Alternatively, you can set environment variables in a CF cli manifest.yml file. If you have a lot to set this can be more convenient as it lets you set them all at once and you don't need to push/start separately, like in the example above. Details on that here.

Hope that helps!

Upvotes: 0

user320487
user320487

Reputation:

You need a package like dotenv you read from the .env file. Your shell environment variables will be available in process.env but node doesn't by default process .env files.

Upvotes: 1

Related Questions