I am developing an app using Laravel + VueJS + Homestead and as everybody knows on Laravel 5.2 we have a env file where we can set env variables... I would like to do something like that but in way where I can access it from my javascript code!
I have read about a proccess.NODE_ENV but I don't know if I got it right but it looks like works only on npm start no? As I am running my app through homestead I don't really know how to do it!
Thanks in advance!
I am developing an app using Laravel + VueJS + Homestead and as everybody knows on Laravel 5.2 we have a env file where we can set env variables... I would like to do something like that but in way where I can access it from my javascript code!
I have read about a proccess.NODE_ENV but I don't know if I got it right but it looks like works only on npm start no? As I am running my app through homestead I don't really know how to do it!
Thanks in advance!
Share Improve this question asked Apr 25, 2016 at 0:18 Gustavo BissolliGustavo Bissolli 1,5713 gold badges23 silver badges36 bronze badges 2-
If you're using Webpack/Browserify you can do something like
var config = require('./path/to/config.json')
. stackoverflow./questions/5869216/… – ceejayoz Commented Apr 25, 2016 at 0:44 - Hi @ceejayoz... My main question is how I set which env I am working! Right now my proccess.NODE_ENV is returning undefined and I can figure out how to make it return the right value! What I am doing is something like proposed below... but I don't know how to make the NODE_ENV return the properly value! – Gustavo Bissolli Commented Apr 25, 2016 at 13:36
2 Answers
Reset to default 8I have a config.js
file that I keep config variables in, like:
const IS_LOCAL = process.env.NODE_ENV !== 'production'
const API_BASE_URL = IS_LOCAL
? 'http://api.domain.dev/v1'
: 'http://api.domain./v1'
const LOGIN_URL = "auth/login"
const LOGOUT_URL = "auth/logout"
const REFRESH_URL = "auth/refresh"
export default {
IS_LOCAL,
API_BASE_URL,
LOGIN_URL,
LOGOUT_URL,
REFRESH_URL
}
Then if I need a config variable in a file I just call:
import {REFRESH_URL, LOGIN_URL} from "./config.js"
You could dump the APP_ENV
environment variable to the page hosting your JavaScript and later access it or pass it to Vue.
<script type="text/javascript">
var env = "{{ env }}";
</script>
and then in your controller...
$env = getenv("APP_ENV");
would get you the value of APP_ENV