Create React App, NPM Build, Add Conditional Content and Production Scripts to Index.html

12/20/2020 8:39:58 PM

Sometimes, when running a create-react-app website, you may want to include additional or different scripts in your production build vs your development build. A very common use case would be to include Google Tag Manager or Google Analytics scripts in your production build. To do so, you can use the NODE_ENV value.

<head>

	<% if (process.env.NODE_ENV === 'production') { %>
		<!-- production -->
		<script src="/my-prod-script.js"></script>
	<% } %>
	<% if (process.env.NODE_ENV === 'development') { %>
		<!-- development -->
		<script src="/my-prod-script.js"></script>
	<% } %>	

</head>