Pass or use the process.env variable from node to response

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

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

in my development, and it works (I think, because it is developing, and I have a backup 'development' .

But when we push it to our intermediate server and set the variable NODE_ENV , it only works on the first boot, but subsequently it is not. I think I get this because it is first served by node and it has access to server variables, but subsequently it will respond to pages (right?), And it will not have access to server materials. So, how do I get variables for a reaction without hard coding (because in the end we will have a different set for production)?

EDIT. We also use webpack if that matters.

+5
source share
2 answers

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 looking for.

+5
source

Webpack also defined EnvironmentPlugin . Just specify an array of names of environment variables and they will be available on the client.

 plugins: [ new webpack.EnvironmentPlugin([ 'NODE_ENV', 'SOME_OTHER_KEY' ]) ] 
+6
source

Source: https://habr.com/ru/post/1247580/


All Articles