Create React App env variables undefined when using Craco build in Heroku
Asked Answered
G

1

8

I'm deploying an Express app (Node.js/React) to Heroku and have set my env vars in Heroku using the config vars in Settings in the Heroku Dashboard. On the server side, I can access them using process.env without any problems. However, in my client, my process.env vars are returning undefined.

I have prefixed them with REACT_APP, and the issue seems to be related to the craco build script in my client/package.json that is called during the build stage of the Heroku deployment. If I set this to react-scripts build, the environment variables behave as expected, however, my TailwindCSS config then fails.

I can also have a .env file in the client, but I need different values depending on the stage of the Heroku pipeline, and NODE_ENV is always "production" once deployed to Heroku so I can't think of a way to have different values depending on the stage.

Is there a way for craco build to get the REACT_APP vars from the Heroku config vars during deployment in the same way react-scripts build does?

Guarnerius answered 23/11, 2021 at 14:26 Comment(0)
E
1

There is a npm package specifically build to use .env variables with craco: https://github.com/djdmbrwsk/dotenv-cra

Erythrism answered 28/7, 2022 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.