score:268
This is best done emulating the Node way of doing things with your build tool - webpack, browserify - by exposing process.env.NODE_ENV
. Typically, you'll have it set to "production" in prod and "development" (or undefined) in dev.
So your code becomes:
if (!process.env.NODE_ENV || process.env.NODE_ENV === 'development') {
// dev code
} else {
// production code
}
For how to set it up, see envify or Passing environment-dependent variables in webpack
score:4
I wanted access to this from the index.html and desired a solution which didn't involve ejecting webpack or configuring it with additional modules and I came up with this.
Sources are David's answer above and the create-react-app documentation for using environment variables in the html file
if ( ! '%NODE_ENV%' || '%NODE_ENV%' === 'development') {
// dev code
} else {
// production code
}
score:22
I use a helper file (in Typescript):
import process from "process";
const development: boolean = !process.env.NODE_ENV || process.env.NODE_ENV === 'development';
export default function isDev(): boolean
{
return development;
}
Then elsewhere I use it like this:
import isDev from "./helpers/DevDetect";
if (isDev())
{
...
}
Source: stackoverflow.com
Related Query
- Detecting production vs. development React at runtime
- Why does production build of React app (with Webpack and Babel) use wrong development env with HMR, which causes errors?
- Using different API url for development and production with React and axios
- React JS: Use environment variables to specify two API urls based on production vs development
- React API calls work in development and fail in production build
- how to use different IP address for development and production with react and axios
- Fetching from localhost server in Create React App during development vs fetching from deployment during production
- react helper import fails in production but works in development
- Create React App production build runtime error: Cannot read property 'call' of undefined
- React - Env variables in development AND production
- When I try to use Parcel to build a production version of a React app that works fine in development mode, I get "Cannot find module 'sass'"
- Webpack production build always returns This page is using the development build of React
- React & Webpack & Docker: Inject env's at runtime in production to the docker-container
- Add production into development React Webpack config
- Deploy React Craco app to GCP Cloud Run Error: React Refresh runtime should not be included in the production bundle
- Setting Up base url for production while using proxy in development in React
- Calling API in production and development in react js
- React component not rendering in production build but works as expected in development build
- How can one tell the version of React running at runtime in the browser?
- Detecting when user scrolls to bottom of div with React js
- Uncaught Error: A cross-origin error was thrown. React doesn't have access to the actual error object in development
- React app stuck on "Starting the development server"
- Why won't React production build run on the browser?
- How to build a production version of React without minification?
- Difference between production and development build in ReactJS
- Is node-sass a dev or a production dependency on React projects?
- How to set React to production mode when using Gulp
- Why is React Webpack production build showing Blank page?
- How can remove console.log in the production build of a React application created using create-react-app?
- Can we share code between react webapp and react native app and is react-native production ready
More Query from same tag
- Assignments to the '[x]' variable from inside React Hook useEffect will be lost after each render
- I am setting a property using setState before dispatching an action, but the property is not set in the redux action on dispatch
- Videojs not working with dynamic URL but working with static URL
- Video element in react does not play the stream
- Toggle Accordion functionality with reactstrap?
- Can I embed a tweet in a post on a React frontend while using headless Wordpress?
- React & Enzyme: why isn't beforeEach() working?
- Where do I put coding logic in my React Application
- componentWillReceiveProps and componentDidUpdate infinite Loop
- Input File's hover effect on ReactJS
- How to resolve @typescript-eslint/no-var-requires error. Error while adding @axe-core/react
- reactjs - render tags accourding with condictionals
- What is causing this JSON to have an unexpected EOF?
- Filtering Data By Full Name React
- Setting up code splitting in Webpack and React.js
- Typescript Compile Error: Type '{}' is not assignable to type 'T'
- How to show html filter/map array with Onclick in React
- Multiple useState in useEffect, order of execution
- Time picker set up in React - jquery-timepicker
- How can I search only the selected options in multivalue react-select?
- Error while rendering an array.map() in React
- How to implement a react controlled input using useReducer?
- Add Babel to Typescript React app
- The best way to pass authorization header in nextJs using Apollo client? ReferenceError: localStorage is not defined
- Changing the arrow/icon colour material ui select
- Why the binding event is executed after the setstate is re-rendered
- How to use spread operator?
- Warning:: ReactDOM.render is no longer supported in React 18. Use createRoot instead
- React router refresh 404
- Axios Get Request not going to Correct URL