Use prod environment file with Ionic 5 React

For angular projects, you can create an environment.ts file and environment.prod.ts file to manage different values for dev and prod builds so that when building with --prod option, the environment.prod.ts file is used when importing environment.

This is not working for ionic 5 with react? Is there a way to achieve this with an Ionic 5 React project?

You can use Environment Variables, as documented in the Create React App docs.

Thanks. This seems to work although every ionic build is production when checking the NODE_ENV variable, so can’t use the --prod flag to create a specific prod build

Yes for React projects the Ionic CLI calls react-scripts, and react-scripts build always creates a production build. (Here’s why.)

2 Likes

Thanks, I’m new to react, but I see why this happens now.

There are workarounds posted on the react forum and documented here. Unfortunately as ionic build always calls react-scripts build as you rightly said, this does not work for ionic.

Nothing is stopping you from using react-scripts directly if you want to.

As ionic-react always build with production config, How can we have 2 env files for the production environment? For example on for the beta channel and one for real production

Like I would like to have one env for developement and one for the production, but When I create android build it will always take the production one