添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
  • Open the vite.config.ts file

  • Add loadEnv in your imports:
    import { defineConfig, loadEnv } from 'vite'

  • Add an env const assigment:
    const env = loadEnv(mode, process.cwd(), '');

  • add a define object at the same level than the plugins array:
    // https://vitejs.dev/config/ export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), ''); return { define: { 'process.env.SOME_KEY': JSON.stringify(env.SOME_KEY) plugins: [react()], Enter fullscreen mode Exit fullscreen mode

    So, if your like me, you'd be like: "but, wait a minute! Am I gonna have to manually write every single new key/value pair in the .env file, AND repeat myself in the vite.config.ts ?"

    Well...this is exactly what this solution implies! I'm not happy about this, but at least it is a solution.

    Why is is important to have this solution? Well because the CI/CD process on Netlify would fail because of the Uncaught ReferenceError: process is not defined error.

    // https://vitejs.dev/config/ export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), ''); return { define: { 'process.env': env plugins: [react()], Enter fullscreen mode Exit fullscreen mode

    You might not wish to expose the entire contents of process.env to the front end, so manually picking the keys/names of the values you wish to get access to in the front, becomes an sort of security device...

    Also, notice that in the after code suggested where you do manually pick them, you might or might not have to stringify the selected keys' values.

    // https://vitejs.dev/config/ export default defineConfig(({ mode }) => { const env = loadEnv(mode, process.cwd(), ''); const processEnv = {}; cherryPickedKeys.forEach(key => processEnv[key] = env[key]); return { define: { 'process.env': processEnv plugins: [react()], Enter fullscreen mode Exit fullscreen mode

    Seems like "process.env" is removed, have to use "import.meta.env" instead.
    So instead of process.env.VITE_SOME_KEY -> import.meta.env.VITE_SOME_KEY

    Read more here - vitejs.dev/guide/env-and-mode.html

    on my side, loadEnv was throwing an error then i revised my code s below. things worked perfectly. thanks for the clue i was totally lost

    import { defineConfig } from 'vite';
    import react from '@vitejs/plugin-react';
    import dotenv from 'dotenv';

    dotenv.config(); // Load environment variables from .env

    // vitejs.dev/config/
    export default defineConfig(({ mode }) => {
    return {
    define: {
    'process.env.REACT_APP_BASE_URL': JSON.stringify(process.env.REACT_APP_BASE_URL),
    plugins: [react()],
    I write about the things I learn in the Frontend ecosystem. Most of them are about Next.js and React. You can find some of my thought pieces on my blog: (https://meje.dev/blog)

    Built on Forem — the open source software that powers DEV and other inclusive communities.

    Made with love and Ruby on Rails . DEV Community © 2016 - 2024.

  •