Removing why-did-you-render from Production
You'll learn how to ensure the why-did-you-render library never ships to your users.
Why Production Removal Matters
why-did-you-render is a powerful debugging tool, but it adds runtime overhead and logs performance data to the console. Your production users don't need this—and shouldn't pay the cost. The library must be completely stripped from production builds to avoid performance degradation and exposing internal debug information.
Environment-Based Conditional Imports
The most common approach uses process.env.NODE_ENV to conditionally load the library only in development:
// wdyr.js (or wherever you configure it)
if (process.env.NODE_ENV === 'development') {
const whyDidYouRender = require('@welldone-software/why-did-you-render');
const React = require('react');
whyDidYouRender(React, {
trackAllPureComponents: true,
});
}
Then import this file in your entry point:
// index.js
if (process.env.NODE_ENV === 'development') {
require('./wdyr');
}
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
// rest of your app
How Build Tools Handle This
Modern bundlers like webpack, Vite, and Create React App automatically:
- Replace
process.env.NODE_ENVwith the literal string'production'during production builds - Tree-shake the entire
ifblock because the condition becomesif ('production' === 'development')(always false) - Remove the dead code entirely from the final bundle
Think of it like a movie director removing behind-the-scenes footage before the final cut—the production version is clean and optimized.
Key Takeaway: Always wrap why-did-you-render imports and initialization in process.env.NODE_ENV === 'development' checks to ensure build tools completely remove it from production bundles.