1

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>`);
1
  • Can you show how you pass your environment variables into your Node instance? Commented Sep 25, 2018 at 8:40

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

1 Comment

Introducing a bundle loader to pass a variable from the server to the client? Bit overkill wouldn't you say?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.