DaraJ
DaraJ

Reputation: 3036

Access environment variables in index.html with webpack

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?

Upvotes: 2

Views: 2788

Answers (1)

Ahmed Wasim
Ahmed Wasim

Reputation: 195

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

Upvotes: 1

Related Questions