如何在 Azure 静态 Web 应用的生产环境中访问我的 React VITE 环境变量?

huangapple go评论77阅读模式
英文:

How can i access my react VITE ENV variables in production of an azure static web app?

问题

locally i use a .env file and do the following:

const value = import.meta.env.VITE_VALUETOGET
console.log("value", value)

When i go to production however i use the azure portal configuration app settings and added VITE_VALUETOGET with the value, this doesn't work because i get undefined if i try to log it.

I also tried to use pipeline to deploy the app and export the values from azure vault before i deploy it but nothing seems to work, process.env neither.

I also tried vite.config.js:

export default defineConfig({
  mode: 'production',
  plugins: [
    react(),

    // Add the replace plugin to inject environment variables
    replace({
      'import.meta.env.VITE_VALUETOGET': JSON.stringify(process.env.VITE_VALUETOGET)
    }),
  ],
});
英文:

locally i use a .env file and do the following:

const value = import.meta.env.VITE_VALUETOGET
console.log("value", value )

When i go to production however i use the azure portal configuration app settings and added VITE_VALUETOGET with the value, this doesn't work because i get undefined if i try to log it.

I also tried to use pipeline to deploy the app and export the values from azure vault before i deploy it but nothing seems to work, process.env neither.

I also tried vite.config.js:

export default defineConfig({
  mode: 'production',
  plugins: [
    react(),

    // Add the replace plugin to inject environment variables
    replace({
      'import.meta.env.VITE_VALUETOGET': JSON.stringify(process.env.VITE_VALUETOGET)
    }),
  ],
});

答案1

得分: 1

  • 从Azure静态Web应用访问env变量 · 讨论变量。在Azure静态Web应用的生产中使用VITE ENV变量。使用此参考进行生产构建
const value = import.meta.env.VITE_VALUETOGET  
console.log('process.env', process.env);
  • 请参考此环境变量和模式
{  
   "NODE_ENV": "production",  
   "PUBLIC_URL": "",  
   "FAST_REFRESH": true  
}
  • 在Vite中使用环境变量。启动命令包含 npx serve -l 8080 .

  • 有关其他详细信息,请参考如何从.env文件中加载环境变量使用Vite

英文:

> Access env variables from Azure Static Web App · Discussion Variables.
VITE ENV variables in the production of an Azure static web app.Used this reference for Building for Production

   const value = import.meta.env.VITE_VALUETOGET  
     console.log('process.env', process.env);
  • Refer this Env Variables and Modes
   {  
     "NODE_ENV": "production",  
     "PUBLIC_URL": "",  
     "FAST_REFRESH": true  
   }

如何在 Azure 静态 Web 应用的生产环境中访问我的 React VITE 环境变量?

如何在 Azure 静态 Web 应用的生产环境中访问我的 React VITE 环境变量?

  • Use Environment Variables in Vite.The startup command contains npx serve -l 8080 .

  • For other details refer this How to load environment variables from .env file using Vite.

huangapple
  • 本文由 发表于 2023年8月4日 05:25:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76831696.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定