SolidJSS
SolidJSโ€ข2y agoโ€ข
4 replies
anhvu0911

Use environment variable in defineConfig

I have an .env.local file
VITE_API_HOST=https://test.com/backend


Before SolidStart 0.4, to define the proxy to this API host, I defined in vite.config.js like so
import solid from "solid-start/vite";
import { defineConfig, loadEnv } from "vite";

export default defineConfig(({ mode }) => {
    const env = loadEnv(mode, process.cwd());

    return {
        plugins: [
            solid({ ssr: false }),
        ],
        server: {
            proxy: {
                "/backend": {
                    target: env.VITE_API_HOST,
                    changeOrigin: true,
                    rewrite: (path) => path.replace(/^\/backend/, ""),
                    secure: false
                },
            },
        },
    };
});


Starting from SolidStart 0.4, the callback is no longer used. I tried the following
import { defineConfig } from "@solidjs/start/config";

export default defineConfig({
    start: {
        ssr: false,
    },
    server: {
        proxy: {
            "/backend": {
                target: process.env.VITE_API_HOST,
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/backend/, ""),
                secure: false
            },
        },
    },
});


But the server can not recognize the process.env.VITE_API_HOST. How can I use environment variables in this case?
Was this page helpful?