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

javascript - How to access environment variables inside React JS web app? - Stack Overflow

programmeradmin3浏览0评论

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). I have read that React sanitises all process.env access.

How to access my custom environment variable (CLUSTER_ENV) inside React 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). I have read that React sanitises all process.env access.

How to access my custom environment variable (CLUSTER_ENV) inside React web app?

Share Improve this question edited May 14, 2018 at 9:27 Martijn 16.1k4 gold badges38 silver badges72 bronze badges asked Sep 19, 2017 at 10:53 SeaWarrior404SeaWarrior404 4,16716 gold badges47 silver badges66 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 13

If you are using webpack, it is possible with Webpack Define plugin.

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.

console.log(NODE_ENV);

edit: not alias, define plugin.

发布评论

评论列表(0)

  1. 暂无评论