Replies: 3 comments 4 replies
|
The catch is that Vite can't load For Vite config values, use export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '')
return {
base: env.VITE_BASE_PATH
}
})If you specifically need the variables in That's basically the distinction. |
|
Fair 😂 Too many words for a simple problem. Short version: |
|
Your Vite intentionally loads Use: import { defineConfig, loadEnv } from 'vite'
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), 'VITE_')
return {
base: env.VITE_BASE_PATH || '/',
}
})So if the requirement is simply "read If you specifically need {
"scripts": {
"dev:vs": "cross-env VITE_BASE_PATH=/my-app/ vite",
"build:vs": "cross-env VITE_BASE_PATH=/my-app/ vite build"
}
}Install export default defineConfig({
base: process.env.VITE_BASE_PATH || '/',
})For a one-off PowerShell launch, the equivalent is: $env:VITE_BASE_PATH = '/my-app/'
npm run devIf ASP.NET Core/SpaProxy is the process starting the npm command, you can instead put One small naming note: keep the The relevant Vite documentation now states this explicitly: https://vite.dev/config/#using-environment-variables-in-config |
Uh oh!
There was an error while loading. Please reload this page.
This is more of a Visual Studio than a Vite problem, but theres nowhere to ask for VS...
I want to set the vite base path using an environment variable however process.env does not contain my .env.development key values until AFTER defineConfig is run
Instead I am manually calling
const env = loadEnv(mode, process.cwd(), 'VITE_');and reading it that way, but that seems wrongIs there some other way?
All reactions