2

I have a React app which has access to environment variables in the normal way using DefinePlugin. We are able to access these variables all throughout the app. However, we cannot access them in the static folder, namely index.html.

Is there any way to access environment variables in static files with Webpack/React?

3

1 Answer 1

1

Try html-webpack-plugin & .ejs template language

new HtmlWebpackPlugin({ iw
     template: './src/public/index.ejs',
     inject: 'body',
     env: process.env.NODE_ENV
}),

<body class="<%= htmlWebpackPlugin.options.env %>"> </body
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer. You're correct but it's unlikely we will be allowed to use ejs

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.