{"id":444,"hash":"17e6f28545ff9629cf0ebf6180270a3b226afda538e2951a01e977d4cc82f038","pattern":"Import.meta.env undefined on production build vitejs","full_message":"I am using Vite to statically compile my React app. However, after building, .env imports become undefined, which is not the case at the development stage.\n\nReading the docs, I've found out that these variables are replaced by their corresponding values, but upon looking at the source/compiled code in the DevTools after serving, it shows an empty object with the .env name/key.\n\nMy vite.config.ts configuration:\n\n//vite.config.ts\nimport { defineConfig, loadEnv } from 'vite';\nimport reactRefresh from '@vitejs/plugin-react-refresh';\nimport { getAliases } from 'vite-aliases';\n\nconst aliases = getAliases({\n  path: 'src',\n  prefix: '@',\n});\n\nexport default ({ mode }) => {\n  process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };\n\n  // import.meta.env.VITE_NAME available here with: process.env.VITE_NAME\n  // import.meta.env.VITE_PORT available here with: process.env.VITE_PORT\n\n  const plugins = mode === 'development' ? [reactRefresh()] : [];\n  return defineConfig({\n    plugins,\n    publicDir: 'src/assets',\n    resolve: {\n      alias: aliases,\n    },\n    build: {\n      chunkSizeWarningLimit: 1500,\n    },\n  });\n};\n\nThe code where I reference these .env vars:\n\n//config.ts\nexport const config = () => {\n  const url = import.meta.env.VITE_SERVER_URL;\n  const api = import.meta.env.VITE_API_ENDPOINT;\n  const auth = import.meta.env.VITE_AUTH_ENDPOINT;\n\n  const isProd = import.meta.env.MODE === 'production';\n  const isDev = import.meta.env.MODE === 'development';\n\n  console.log(url, api, auth);\n  return {\n    api: (endpoint: string) => `${url}${api}${endpoint}`,\n    auth: (endpoint: string) => `${url}${auth}${endpoint}`,\n    test: (endpoint: string) => `${url}test${endpoint}`,\n    isProd,\n    isDev,\n  };\n};","ecosystem":"npm","package_name":"node.js","package_version":null,"solution":"I just realized what the ViteJS documentation says and I'll leave it in case someone also suffers from this.\n\nTo prevent accidentally leaking env variables to the client, only variables prefixed with VITE_ are exposed to your Vite-processed code","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/67378099/import-meta-env-undefined-on-production-build-vitejs","votes":51,"created_at":"2026-04-19T04:51:09.960378+00:00","updated_at":"2026-04-19T04:51:09.960378+00:00"}