# Prevent console from being output only in nextjs production environment

日本語版: 日本語で読む

Table of Contents

conclusion

/** @type {import('next').NextConfig} */
const nextConfig = {
// Error: "next start" does not work with "output: export" configuration. Use "npx serve@latest out" instead.
// output: "export",
reactStrictMode: false,
compiler: {
removeConsole: process.env.NODE_ENV === "production",
},
};
export default nextConfig;

In the above code, production of nextjs console is no longer output when building. I made it.

I can’t proceed without debugging

It wouldn’t be a problem if you were an expert who could understand what was going on by looking at the code, but if you don’t have that kind of technical ability, you can’t proceed unless you keep issuing console and checking what’s going on where.

Because of this, the console screen is filled with a large amount of logs.

Naturally, in such a situation, I can’t deploy, so it’s a pain to delete console each time, and I want to use console in the development environment as is.

So, I want to delete it only when deploying.

I want to turn off the console only in the production environment

For nextjs, there is a way to not output console only during deployment in the settings, so I did it.

I think most frameworks have it.

Configure the following settings in next.config.js.

module.exports = {
compiler: {
removeConsole: process.env.NODE_ENV === "production",
},
};

I thought I needed to define NODE_ENV myself, but

Referring to official,

The greater React ecosystem treats NODE_ENV as a convention, only permitting three (3) values:

production: When your application```` is built with next build development: When your application is run with next dev test: When your application is being tested (e.g. jest)

It looks like it’s predefined like this.

Now you can output more and more console.


More Posts
My avatar

Thanks for reading my blog post! Feel free to check out my other posts or contact me via the social links in the footer.


Comments