admin管理员组文章数量:1024174
I am trying to configure my vite/React app so that we can put the server address in an environment variable.
I am trying to store the server address in env.local
and loading it with loadEnv
but I can't get the environment variables to load.
If I hard code my server address it works fine.
My env.local file just contains
VITE_API_URL=http://120.0.0.1:8080
Here is my vite.config.ts:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import { TanStackRouterVite } from '@tanstack/router-vite-plugin';
import { loadEnv } from 'vite';
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
if (!env.VITE_API_URL) {
throw new Error(`VITE_API_URL is not defined <${mode}> ${JSON.stringify(env)}`);
}
return {
plugins: [TanStackRouterVite(), react()],
base: '/app/',
server: {
proxy: {
// '/api': 'http://127.0.0.1:8080', <<-- this works
'/api': env.VITE_API_URL,
},
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
build: {
sourcemap: true,
outDir: 'build',
},
};
});
I am trying to configure my vite/React app so that we can put the server address in an environment variable.
I am trying to store the server address in env.local
and loading it with loadEnv
but I can't get the environment variables to load.
If I hard code my server address it works fine.
My env.local file just contains
VITE_API_URL=http://120.0.0.1:8080
Here is my vite.config.ts:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import { TanStackRouterVite } from '@tanstack/router-vite-plugin';
import { loadEnv } from 'vite';
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
if (!env.VITE_API_URL) {
throw new Error(`VITE_API_URL is not defined <${mode}> ${JSON.stringify(env)}`);
}
return {
plugins: [TanStackRouterVite(), react()],
base: '/app/',
server: {
proxy: {
// '/api': 'http://127.0.0.1:8080', <<-- this works
'/api': env.VITE_API_URL,
},
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
build: {
sourcemap: true,
outDir: 'build',
},
};
});
The exception throws. If I remove the exception my calls to the back end won't work.
Share Improve this question asked Nov 19, 2024 at 10:52 ohthepainohthepain 2,0803 gold badges21 silver badges34 bronze badges 2- "The exception throws" - Which exception? You haven't added any in the question. Please edit and add the full error/exception as text inside it. – DarkBee Commented Nov 19, 2024 at 11:06
- this is answered stackoverflow/questions/79203480/… – Irrfan23 Commented Nov 19, 2024 at 13:00
1 Answer
Reset to default -1Place your .env.local file in the root directory of your project, at the same level as your vite.config.ts file
I am trying to configure my vite/React app so that we can put the server address in an environment variable.
I am trying to store the server address in env.local
and loading it with loadEnv
but I can't get the environment variables to load.
If I hard code my server address it works fine.
My env.local file just contains
VITE_API_URL=http://120.0.0.1:8080
Here is my vite.config.ts:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import { TanStackRouterVite } from '@tanstack/router-vite-plugin';
import { loadEnv } from 'vite';
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
if (!env.VITE_API_URL) {
throw new Error(`VITE_API_URL is not defined <${mode}> ${JSON.stringify(env)}`);
}
return {
plugins: [TanStackRouterVite(), react()],
base: '/app/',
server: {
proxy: {
// '/api': 'http://127.0.0.1:8080', <<-- this works
'/api': env.VITE_API_URL,
},
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
build: {
sourcemap: true,
outDir: 'build',
},
};
});
I am trying to configure my vite/React app so that we can put the server address in an environment variable.
I am trying to store the server address in env.local
and loading it with loadEnv
but I can't get the environment variables to load.
If I hard code my server address it works fine.
My env.local file just contains
VITE_API_URL=http://120.0.0.1:8080
Here is my vite.config.ts:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import path from 'path';
import { TanStackRouterVite } from '@tanstack/router-vite-plugin';
import { loadEnv } from 'vite';
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
if (!env.VITE_API_URL) {
throw new Error(`VITE_API_URL is not defined <${mode}> ${JSON.stringify(env)}`);
}
return {
plugins: [TanStackRouterVite(), react()],
base: '/app/',
server: {
proxy: {
// '/api': 'http://127.0.0.1:8080', <<-- this works
'/api': env.VITE_API_URL,
},
},
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
},
},
build: {
sourcemap: true,
outDir: 'build',
},
};
});
The exception throws. If I remove the exception my calls to the back end won't work.
Share Improve this question asked Nov 19, 2024 at 10:52 ohthepainohthepain 2,0803 gold badges21 silver badges34 bronze badges 2- "The exception throws" - Which exception? You haven't added any in the question. Please edit and add the full error/exception as text inside it. – DarkBee Commented Nov 19, 2024 at 11:06
- this is answered stackoverflow/questions/79203480/… – Irrfan23 Commented Nov 19, 2024 at 13:00
1 Answer
Reset to default -1Place your .env.local file in the root directory of your project, at the same level as your vite.config.ts file
本文标签: reactjsReactVite won39t load envlocalStack Overflow
版权声明:本文标题:reactjs - ReactVite won't load env.local - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745567224a2156523.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论