最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

typescript - Any mention of Env breaks Vite app on production - Stack Overflow

programmeradmin1浏览0评论

When I try to add in any mention of import.meta.env I get the a fatal error.

const backendUrl = import.meta.env.VITE_BACKEND_URL;

TypeError: Cannot read properties of undefined (reading 'VITE_BACKEND_URL').

I can see the env without any issues inside the vite.config.ts when I log it, but it won't go any further. This is my vite.config.ts:

import { vitePlugin as remix } from '@remix-run/dev';
import { defineConfig, loadEnv } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
import { cjsInterop } from 'vite-plugin-cjs-interop';
import path from 'path';
import { nodePolyfills } from 'vite-plugin-node-polyfills';

const __dirname = path.resolve();

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, __dirname, 'VITE_');
  const isDevelopment = mode === 'development';

  return {
    plugins: [
      remix({
        appDirectory: 'app',
        buildDirectory: 'build',
      }),
      tsconfigPaths(),
      cjsInterop({
        dependencies: ['react-color'],
      }),
      nodePolyfills()
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './app'),
      },
    },
    server: {
      host: env.VITE_HOST || 'localhost',
      port: parseInt(env.VITE_PORT || '5173'),
      ...(isDevelopment && {
        watch: {
          usePolling: true,
        },
      }),
    },
    build: {
      outDir: 'dist',
    },
    publicDir: 'public',
    optimizeDeps: {
      entries: ['./app/entry.client.tsx', './app/root.tsx', './app/routes/**/*'],
    },
  };
});

What can I try next to resolve this?

When I try to add in any mention of import.meta.env I get the a fatal error.

const backendUrl = import.meta.env.VITE_BACKEND_URL;

TypeError: Cannot read properties of undefined (reading 'VITE_BACKEND_URL').

I can see the env without any issues inside the vite.config.ts when I log it, but it won't go any further. This is my vite.config.ts:

import { vitePlugin as remix } from '@remix-run/dev';
import { defineConfig, loadEnv } from 'vite';
import tsconfigPaths from 'vite-tsconfig-paths';
import { cjsInterop } from 'vite-plugin-cjs-interop';
import path from 'path';
import { nodePolyfills } from 'vite-plugin-node-polyfills';

const __dirname = path.resolve();

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, __dirname, 'VITE_');
  const isDevelopment = mode === 'development';

  return {
    plugins: [
      remix({
        appDirectory: 'app',
        buildDirectory: 'build',
      }),
      tsconfigPaths(),
      cjsInterop({
        dependencies: ['react-color'],
      }),
      nodePolyfills()
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, './app'),
      },
    },
    server: {
      host: env.VITE_HOST || 'localhost',
      port: parseInt(env.VITE_PORT || '5173'),
      ...(isDevelopment && {
        watch: {
          usePolling: true,
        },
      }),
    },
    build: {
      outDir: 'dist',
    },
    publicDir: 'public',
    optimizeDeps: {
      entries: ['./app/entry.client.tsx', './app/root.tsx', './app/routes/**/*'],
    },
  };
});

What can I try next to resolve this?

Share Improve this question edited Nov 19, 2024 at 16:15 Drew Reese 205k18 gold badges246 silver badges274 bronze badges asked Nov 19, 2024 at 12:46 NeficNefic 434 bronze badges 1
  • You can check out the answer in this link, see if it helps stackoverflow/questions/78381012/… – Funmilayo Talabi Commented Nov 19, 2024 at 14:03
Add a comment  | 

1 Answer 1

Reset to default -1

you need to load the .env like this

  const env = loadEnv(process.env.NODE_ENV, process.cwd(), "");
  return {
    plugins: [react(), legacy()],
    define: {
      __APP_ENV__: env,
      "process.env": env
    },
  };
发布评论

评论列表(0)

  1. 暂无评论