Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Environment Variables In VitePress - issue#4516 #4517

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions docs/en/reference/environment-variables.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
outline: deep
---

# Environment Variables

## Basic Workflow

Using environment variables in VitePress is essential for managing sensitive data such as API keys and service URLs.
Copy link
Contributor

@skirtles-code skirtles-code Jan 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this could be misleading.

While it depends on the specifics of the use case, using environment variables won't protect sensitive data in most cases. Usually the values of the variables end up in the built JS files, where anyone can access them.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's true
I used the API keys as GitHub secrets in my CI/CD pipeline but still these values need to be read from the environment even if they are not written in the .env file.

Could you think of a better, more specific example?

VitePress allows you to load environment variables from `.env` files using the `loadEnv` function to load these variables into your configuration (e.g. `.vitepress/config.ts`).

### Step 1: Create the .env File

The first step in setting up environment variables is to create a `.env` file in the root directory of your VitePress project.
This file will store your sensitive information and configuration settings.

### Step 2: Defining Environment Variables

Open the `.env` file and define your environment variables.
Remember that in Vite, all environment variables that need to be exposed to your application must be prefixed with `VITE_.` Here’s an example of what your `.env` file might look like

```bash
VITE_ALGOLIA_APP_ID=your_algolia_app_id
VITE_ALGOLIA_API_KEY=your_algolia_api_key
VITE_ALGOLIA_INDEX_NAME=your_algolia_index_name
```

### Step 3: Load Environment Variables in VitePress Configuration

Next, you need to load these environment variables into your VitePress configuration. This is done using the `loadEnv` function from VitePress.

```js
import { loadEnv } from 'vitepress';

// Load environment variables
const env = loadEnv('', process.cwd());
```

### Step 4: Accessing Environment Variables in Your Application

for example, configuring your blog to use Algolia

```js
import { loadEnv } from 'vitepress';
import { defineConfig } from 'vitepress';

// Load environment variables
const env = loadEnv('', process.cwd());

export default defineConfig({
themeConfig: {
search: {
provider: 'algolia',
options: {
appId: env.VITE_ALGOLIA_APP_ID,
apiKey: env.VITE_ALGOLIA_API_KEY,
indexName: env.VITE_ALGOLIA_INDEX_NAME,
},
},
},
});

```

## Important Considerations

- **Prefix Requirement**: Ensure all relevant environment variables are prefixed with `VITE_`. Only these will be accessible in your application.
- **File Loading Order**: Vite loads `.env` files based on specific priorities. For example, `.env.production` will override settings from `.env`.
- **Restarting Your Server**: After making changes to your `.env` file, restart your VitePress server for changes to take effect.