Environments
The environments are managed by Vite as explained at https://vitejs.dev/guide/env-and-mode.html.By default, the dev server (
npm run dev
command) runs in development mode and the build command npm run build
runs in production mode.
Environment files
There are two environment files: "src/.env.development" and "src/.env.production".
Only variables prefixed with DODO_ will be exposed to the app. It's defined in the vite.config.js.
How to use
The environment variable defined in the src/.env.development can be used withimport.meta.env.DODO_VARNAME
.
Let's see in the example:
"src/.env.development"
------------------------------------------------
DODO_API_BASE="http://127.0.0.1:3336" # will be exposed to app during Vite compilation due to DODO_ prefix
SECRET_KEY="12345" # will not be exposed to app
Now you can use DODO_API_BASE in the app.js and also in the controller files as
import.meta.env.DODO_API_BASE
Production on dev server
During development the variables from ".env.development" file will be loaded into the app.However sometimes we will want to use ".env.production" on dev server.
In that case the command
npx vite --config vite.config.js --mode production
or just npm run prod
can be used.
Mode
There are two default .dev files. When more dev files are needed (for example ".env.stage"), the --mode option must be used.For example:
npx vite --config vite.config.js --mode stage
or
npx vite build --mode stage