score:268

Accepted answer

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())
{
    ...
}

Related Query

More Query from same tag