Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To include process.env while utilizing Vite, follow these steps:

  1. Create a .env file at the root of your project directory.
  2. Define your environment variables in the .env file in the format NAME=VALUE, for example:
VITE_API_KEY=123456789
VITE_BASE_URL=https://example.com
  1. In your Vite configuration file (vite.config.js), import the dotenv package:
import dotenv from 'dotenv'
  1. Call the config method of the dotenv package to load the environment variables:
dotenv.config()
  1. Define the environment variables in the Vite configuration file using the process.env object:
export default {
  // other Vite config options
  plugins: [],
  define: {
    'process.env': process.env
  }
}
  1. You can now access the environment variables in your code using process.env.VITE_API_KEY and process.env.VITE_BASE_URL.