Search code examples
reactjsreact-reduxredux-middleware

Add react middleware when not in production


The question is simple,

How I can add a react or redux middle-ware (like Logger) when not in production.

I don't want users be able to see the logging info in the console tab.


Solution

  • Let say you have 2 middlewares, logger and thunk and you want to add logger when you are not in production, you can do that using following code:

    const middleware = [thunk];
    if (process.env.NODE_ENV !== 'production') {
        middleware.push(logger);
    }
    

    And you can set NODE_ENV to production using following code:

    set NODE_ENV=production
    

    Or using PowerShell:

    $env:NODE_ENV="production"
    

    Set within package.json (you need to install cross-env, more info right here: How to set Environment variables from within package.json [Node.js]):

    "scripts": {    
        "build": "cross-env NODE_ENV=production webpack -p"
    }