Skip to main content

How to Resolve the "Unexpected console statement" ESLint Error (no-console) in JavaScript

The ESLint error Unexpected console statement (no-console) is a common linting issue that occurs when you have calls to console methods (like console.log, console.warn, etc.) in your codebase. This rule is enabled by default in many ESLint configurations to prevent logging statements from being accidentally left in production code.

This guide will explain why the no-console rule exists and walk you through the various ways to resolve this error, from disabling it for a specific line to configuring it for your entire project.

Why Does the no-console Rule Exist?

Calls to console.log() and other console methods are primarily used for debugging during development. It is generally considered a bad practice to ship code to production that contains these statements. They can:

  • Clutter the browser console for other developers or even end-users.
  • Potentially leak sensitive information if an object with private data is logged.
  • Be a sign of unfinished debugging work.

The no-console rule helps enforce a clean codebase by flagging these statements, reminding you to remove them before committing your code.

Solution 1 (For Specific Lines): Using ESLint Disable Comments

Sometimes, you have a legitimate reason to use console.log (e.g., for logging important, non-sensitive information in a Node.js script). In these cases, you can disable the rule for a specific line or block of code.

Solution for a single line

Place a special comment on the line before the console call.

// eslint-disable-next-line no-console
console.log('Starting the application...');

Solution for a block of code

You can wrap a section of your code with comments to disable and then re-enable the rule.

/* eslint-disable no-console */
console.log('--- Begin Diagnostic Info ---');
console.log('User ID:', userId);
console.log('State:', currentState);
/* eslint-enable no-console */

Solution 2 (For an Entire File): A File-Level Disable Comment

If you have a file that is specifically for logging or a utility script where console calls are expected, you can disable the rule for the entire file.

Place the following comment at the very top of your file.

/* eslint-disable no-console */

console.log('This is a utility script.');
// ... many other console calls ...
console.log('Script finished.');

Solution 3 (Project-Wide): Configuring the .eslintrc File

If you want to change the behavior of the no-console rule for your entire project, the best place to do so is in your ESLint configuration file (e.g., .eslintrc.js or .eslintrc.json).

For server-side applications where logging to the console is a primary function, it's often practical to turn this rule off completely.

In .eslintrc.js:

module.exports = {
// ... other ESLint config
rules: {
'no-console': 'off',
},
};

Option B: Downgrade the Rule to a Warning

This is a good compromise. It will still remind you about console calls during development but will not fail your build process.

// In .eslintrc.js
rules: {
'no-console': 'warn',
},

Option C: Allow Specific console Methods

You can configure the rule to allow certain methods (like .warn and .error) while still flagging others (like .log).

// In .eslintrc.js
rules: {
'no-console': ['warn', { allow: ['warn', 'error'] }],
},

This configuration would show a warning for console.log() but would allow console.warn() and console.error().

Conclusion

The no-console ESLint rule is a valuable tool for maintaining a clean, production-ready codebase.

  • For temporary debugging, using console.log and then removing it is the best approach.
  • For specific, intentional logging, use an inline disable comment like // eslint-disable-next-line no-console.
  • For project-wide changes, modify your .eslintrc.js file. A common and highly effective strategy is to set the rule to 'warn' or to disable it completely for back-end projects.

By understanding how to manage this rule, you can keep your console clean while still using console for legitimate purposes.