How to Remove console.log in Production in Next.js
We always tend to forget to remove console.log
statements before pushing our code to production. While these logs are useful for debugging, they can clutter the console, expose unnecessary details, and even affect performance. Instead of manually removing them every time, why not be smart and automate the process? Next.js provides a simple built-in way to strip out console.log
statements in production.
Steps:
- Open or create a
next.config.js
file in your project’s root directory. - Add the following configuration:
module.exports = { compiler: { removeConsole: process.env.NODE_ENV === "production", }, };
Explanation:
- The
removeConsole
option ensures that allconsole.log
statements are removed when building for production. - This method is simple and requires no additional plugins or dependencies.
Conclusion
By using the removeConsole
option in next.config.js
, you can easily prevent console.log
statements from appearing in production builds, keeping your application clean and efficient.