Use prod environment file with Ionic 5 React

For angular projects, you can create an environment.ts file and file to manage different values for dev and prod builds so that when building with --prod option, the 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.)

1 Like

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.