I am currently utilizing a standard Vue starter webpack template with VueAxios integration. My aim is to set up axios configuration using .env variables specifically when building with the dev
command. I have everything structured within the /config/index.js
:
const devEnv = require('./dev.env')
module.exports = {
...
host: devEnv.host || 'localhost',
port: devEnv.port || 8080,
...
}
Subsequently, I establish my host and port in dev.env.js
located in the same directory, and it operates seamlessly.
const dotenv = require('dotenv').config({path: '../.env'})
let host = process.env.VUE_HOST;
let port = +process.env.VUE_PORT;
module.exports = merge(prodEnv, {
NODE_ENV: '"development"',
host,
port
})
However, the issue arises when trying to access the host value within src/main.js
of my Vue application.
When attempting it like this, an error message is returned stating:
vue is not defined
import axios from 'axios'
import VueAxios from 'vue-axios'
...
Vue.use(VueAxios, axios)
Vue.axios.defaults.baseURL = `http://${process.env.host}:${process.env.port}`;
While the port functions correctly, the host triggers an error.