传递或使用process.env变量从节点到reactjs [英] Pass or use process.env variable from node to reactjs

查看:3566
本文介绍了传递或使用process.env变量从节点到reactjs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何传递或使用 process.env 变量从节点到reactjs?例如我有这个



const nodeEnv = process.env.NODE_ENV || 'development'



在我的开发中,它的工作原理(我认为,因为它的开发和我有一个后退但是当我们将它推送到我们的临时服务器并设置 NODE_ENV 时,变量,它只工作在第一次加载,但后来它不,我想我得到这是因为首先它由节点提供服务,它可以访问服务器变量,但之后,它将是reactjs服务页面(右?)并且它不能访问服务器的东西。所以我如何得到变量reactjs没有硬编码它(因为我们最终会有一个不同的设置为生产)?


解决方案

1分钟搜索google,我发现这个: http://dev.topheman.com/make-your-react -production-minified-version-with-webpack /

  module.exports = {
// ...
plugins:[
new webpack.DefinePlugin({
'process.env':{
'NODE_ENV':JSON.stringify('production')
}
}),
// [...]
]
// ...
}

在我看来,这正是你正在寻找的。


How do I pass or use process.env variables from node to reactjs? For example I have this

const nodeEnv = process.env.NODE_ENV || 'development'

in my development and it works (I think because it's development and I DO have a fallback 'development'.

But when we push it to our staging server and set NODE_ENV variable, it only works the first time it loads but subsequently it doesn't. I think I do get this because at first it's served by node and it has access to server variables but afterwards it would be reactjs serving the pages (right?) and it wouldn't have access to server stuff. So how do I get to have variables to reactjs without hardcoding it (because we would eventually have a different set for production)?

EDIT. We also use webpack if that has a difference.

解决方案

1min searching google and I found this: http://dev.topheman.com/make-your-react-production-minified-version-with-webpack/

module.exports = {
  //...
  plugins:[
    new webpack.DefinePlugin({
      'process.env':{
        'NODE_ENV': JSON.stringify('production')
      }
    }),
    // [...]
  ]
  //...
}

In my opinion this is exactly what you are searching for.

这篇关于传递或使用process.env变量从节点到reactjs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆