15

When I manually selected presets when installing vue using vue cli. I stumbled upon process.env.BASE_URL. I was tryng to find it in the internet but to no avail, I can't find any decent explanation. Here's the code.

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
Errol Paleracio
  • 614
  • 1
  • 7
  • 21

1 Answers1

9

the documentation is here https://cli.vuejs.org/guide/mode-and-env.html#environment-variables

It has a system of cascading files that will determine enviroment variables for your app. The main file being a .env

The BASE_URL is semi arbitary, you could introduce your own BASE_ROUTER_URL variable if you wanted

Keith Nicholas
  • 43,549
  • 15
  • 93
  • 156
  • 3
    I do not see any `.env` file in my project root. Where is it actually ? – Istiaque Ahmed Jan 04 '20 at 19:00
  • via vue ui? I don't think it makes them by default, so just make it yourself. Read the info in the link. It has a pretty good description of how they work – Keith Nicholas Jan 05 '20 at 01:59
  • 1
    no, i created the project via `vue create project_name`. No `.env` file in the project root. Then how could it use `process.env` in the router as said in OP ? – Istiaque Ahmed Jan 05 '20 at 11:32
  • 22
    `BASE_URL` is not arbitrary. ([doc](https://cli.vuejs.org/guide/mode-and-env.html#using-env-variables-in-client-side-code)) `BASE_URL` - this corresponds to the `publicPath` option in `vue.config.js` and is the base path your app is deployed at. – Allan Chain May 23 '20 at 06:03
  • you need to create the `.env` file yourself. You should have different `.env` file for production `.env` and development: `.env.development`. You need to use the `vue-cli-service serve --mode development` to run your project in development mode. – hoomi Jul 24 '22 at 12:45