dotenv not working when deploy React code to S3

Viewed 324

I have a .env file with one variable API_BASE_URL. And I use webpack, dotenv and webpack.DefinePlugin to build the react app. The code is working on my localhost and my local server by serve dist using http-server. When I deploy my to S3 using AWS codepipeline, even though I set API_BASE_URL on AWS codebuild part, but the deployed app on S3 cannot load the variable. Anyone knows where is wrong?

webpack:

const dotenv = require("dotenv")
module.exports = () => {
    const env = {} || dotenv.config().parsed
    const envKeys = Object.keys(env).reduce((prev, next) => {
        prev[`${next}`] = JSON.stringify(env[next])
        return prev
    }, {})
    ...
    plugins: [
        new webpack.DefinePlugin(envKeys),
        new HtmlWebpackPlugin({
            template: "./public/index.html",
        }),
    ]
...

.env(it's on the root directory)

API_BASE_URL = xxxxxx

App file

const App = () => {
    console.log("API_BASE_URL", API_BASE_URL)
    return (
...

set enviroment variable on aws enter image description here

cannot find the varible on S3

enter image description here

1 Answers
Related