English 中文(简体)
Ract Vite 读取 ENVV 变量和消防基地
原标题:React Vite reading ENV variables and firebase

我有一个使用 Firebase / Firestore 的 React / VITE 项目。 我的问题是在 producting 中读取环境变量。 一切都在本地/开发模式下运行良好。 此项目在构建时没有错误。 下面是我在生产过程中看到的错误。 您可以看到我正在记录 < code> import.meta.env 来查看我们是否有. env 变量。 我还尝试了其他相关的 Stack overflow 文章 。

这正是我为这一点所做的。

  1. I made sure the variables start with VITE_XX as per VITE S documentation here
  2. I tried defining the prefix in the envPrefix property with something else
  3. I tried changing the envDir property to / thinking VITE didn t know where to look for the .env file. It defaults to the root directly soooo.
  4. To make sure it wasn t my server, I created a blank React app (CRA) without VITE and added a .env to the root with some variables and sent the build to production and it read the .env file fine. So I have reason to believe the issue is with VITE configuration and not my server.
  5. .env file is located in the root of the project NOT the src directory.
  6. I added a console.log in the ./src/config/firebase.ts file to verify the variables are getting read in. Seeing all variables read in locally but not production.

这里的是我的当前 vite. config.ts config.ts config。 正如您所看到的,我做了一些修改,以便使用 process.env ,而不是VITE s docs 中建议的 import.meta.env 。我尝试了他们的方法,但毫无运气。同样,这个方法在. env 变量中在当地读到精细,但在两种情况下都没有在生产中读到。

import { defineConfig, loadEnv } from  vite 
import react from  @vitejs/plugin-react 
import path from  path 
import dynamicImport from  vite-plugin-dynamic-import 
import tsconfigPaths from  vite-tsconfig-paths 

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
    const env = loadEnv(mode, process.cwd(),   )
    return {
        base: env.VITE_ROUTER_BASE_URL ||  / ,
        define: {
             process.env : env,
        },
        envDir:  ./ ,
        plugins: [
            react({
                babel: {
                    plugins: [ babel-plugin-macros ],
                },
            }),
            dynamicImport(),
            tsconfigPaths(),
        ],
        assetsInclude: [ **/*.md ],
        resolve: {
            alias: {
                 @ : path.join(__dirname,  src ),
            },
        },
        build: {
            outDir:  build ,
        },
    }
})

这里的 ./src/config/firebase.ts 文件

import { initializeApp } from  firebase/app 
import { getAuth } from  firebase/auth 
import { getFirestore } from  firebase/firestore 

console.log( VITE VARS , process.env)

const app = initializeApp({
    apiKey: process.env.VITE_FIREBASE_API_KEY,
    authDomain: process.env.VITE_FIREBASE_AUTH_DOMAIN,
    projectId: process.env.VITE_FIREBASE_PROJECT_ID,
    storageBucket: process.env.VITE_FIREBASE_STORAGE_BUCKET,
    messagingSenderId: process.env.VITE_FIREBASE_MESSAGE_SENDER_ID,
    appId: process.env.VITE_FIREBASE_APP_ID,
})

export const auth = getAuth(app)
export const firestore = getFirestore(app)

export default app

最后我的 .env 位于项目根目录中。

VITE_FIREBASE_API_KEY=xxx
VITE_FIREBASE_AUTH_DOMAIN=xxx
VITE_FIREBASE_PROJECT_ID=xxx
VITE_FIREBASE_STORAGE_BUCKET=xxx
VITE_FIREBASE_MESSAGE_SENDER_ID=xxxx
VITE_FIREBASE_APP_ID=1:xxxx
问题回答

全部工作都如预期一样。 结果发现我的 GitHub CI / CD 管道没有上传所有所需的文件。 如果您遇到这个问题, 请尝试手动上传构建文件 。





相关问题
How to use one react app into another react app?

I have two react apps, parent app and child app. and child app have an hash router. I have build the child app using npm run build, It creates build folder. That build folder moved into inside the ...

how to get selected value in Ant Design

I want to print the selected value, and below is my option list: const vesselName = [ { value: 0 , label: ALBIDDA , }, { value: 1 , label: ALRUMEILA , }, { value: 2 ,...

How to add a <br> tag in reactjs between two strings?

I am using react. I want to add a line break <br> between strings No results and Please try another search term. . I have tried No results.<br>Please try another search term. but ...

热门标签