简体   繁体   English

如何访问 React JS Web 应用程序中的环境变量?

[英]How to access environment variables inside React JS web app?

I have an environment variable that I need to access inside my render method.我有一个需要在渲染方法中访问的环境变量。 Since its a custom ENV variable, I cannot use (process.env.NODE_ENV).由于它是一个自定义 ENV 变量,我不能使用 (process.env.NODE_ENV)。 I have read that React sanitises all process.env access .读过 React 清理了所有 process.env 访问

How to access my custom environment variable (CLUSTER_ENV) inside React web app?如何在 React Web 应用程序中访问我的自定义环境变量 (CLUSTER_ENV)?

If you are using webpack, it is possible with Webpack Define plugin.如果您正在使用 webpack,则可以使用Webpack Define插件。

webpack.config.js: webpack.config.js:

    ...
    plugins: [
     new webpack.DefinePlugin({
       'NODE_ENV': JSON.stringify(process.env.NODE_ENV)
     })
    ]      
    ...

and then simply you can use on your javascript file.然后你就可以在你的 javascript 文件上使用。

console.log(NODE_ENV);

edit: not alias, define plugin.编辑:不是别名,定义插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Nuxt 中访问 a.js 文件中的环境变量 - How to access environment variables in a .js file in Nuxt Python Flask 应用程序:如何访问 java 脚本文件中的容器环境变量 - Python Flask App: how to access container environment variables inside java script file REACT APP 无法访问谷歌云运行环境变量 - REACT APP not access the google cloud run environment variables 如何在反应应用程序和将其托管为静态站点的 express js 服务器之间共享环境变量 - how to share environment variables between react app and express js server hosting it as static site 如何在组件内部使用React自定义环境变量 - How to use React Customize Environment Variables inside component 如何访问app.get中的变量 - how to access variables inside app.get 如何在客户端访问环境变量(Node.js) - How to access environment variables client-side (Node.js) 如何将Node环境变量传递给React Native JS代码 - How to pass Node environment variables to React Native JS code 如何在 vite.config.js 中为 React 项目添加环境变量 - How to add Environment Variables in vite.config.js for a React Project 如何从 Node 应用程序客户端访问环境变量 - How to access environment variables from Node app client
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM