user3137360
user3137360

Reputation: 11

How I access process.env variables in react html file

I have HTML file as follows below and process.env variables as process.env.var1='abc' but once application is loaded the variable is undefined.

res.send(`
   <html>
   <head>
       <link href="${process.env.var1}" rel="stylesheet">

       </script>

   </head>
   <body>
      <div id="react-root"></div>
  </body>
</html>`);

Upvotes: 1

Views: 3928

Answers (1)

Alok Rai
Alok Rai

Reputation: 95

process.env is a global variable provided by your environment through Node Js. But we don't have Node js in the browser, we are going to use webpack.

So you can use configure webpack DefinePlugin to create global constants

// webpack.config.js
const webpack = require('webpack');

module.exports = {
  entry: './index.js',
 plugins: [
     new webpack.DefinePlugin({
        'process.env': {
            'NODE_ENV': '"production"'
        }
    })
 ]
};

//index.js
console.log(process.env);

console.log(process.env.NODE_ENV);

Upvotes: 2

Related Questions