Using ESLint’s no-console Rule to Keep Production Code Clean
Learn why the no-console rule is essential for production builds, how to configure it for selective use, avoid common pitfalls, and verify it in CI. A practical guide with examples.
02 Jun 2026, 14:09 UTC

Why the no-console Rule Matters
In a team that ships JavaScript to browsers or Node.js, stray console.log statements can leak debugging output into end‑user traffic. ESLint’s no-console rule marks any console method call as an error unless explicitly whitelisted. By catching these calls at compile time, teams ensure that production bundles remain uncluttered and that logs are handled by a proper logging framework.
Configuring the Rule
The rule is simple to enable. In .eslintrc.json:
{
"rules": {
"no-console": "error"
}
}
This turns every console method (log, warn, error, etc.) into a lint error. If you need to allow specific methods, pass an options object:
{
"rules": {
"no-console": ["error", { "allow": ["warn", "error"] }]
}
}
Here, console.warn and console.error are exempt, while console.log remains forbidden.
Environment-Based Overrides
During local development, console statements are handy. ESLint lets you enable or disable rules per environment. Create a .eslintrc.development.json that extends the base config:
{
"env": { "development": true },
"extends": "./.eslintrc.json",
"rules": {
"no-console": "off"
}
}
Run the linter with the --env development flag, or set NODE_ENV=development before executing your build. In production, the base config remains active, enforcing the rule.
Common Pitfalls
- Overly Broad Overrides: Setting
no-consoletooffin a shared config can allow console calls in the production build if the wrong config file is referenced. - Polyfilled Console Methods: Some libraries polyfill
console(e.g.,console.polyfill()). The rule will flag these unless the specific method is whitelisted inallow. - Missing Environment Flags: When CI runs
eslint src/**/*.jswithout--env, it defaults tonodeandbrowser, which may not match the environment you intended to override.
Verification Workflow
To confirm the rule works as expected, follow these steps:
- Create a test file:
// test.js console.log('debug'); console.warn('warning'); console.error('error'); - Run ESLint with the base config:
npx eslint test.jsExpected output: an error for
console.logonly, unlessallowincludes it. - Run with the development override:
npx eslint --env development test.jsExpected output: no errors.
- Integrate into CI:
# .github/workflows/ci.yml - name: Lint run: npx eslint src/**/*.js --quietAny console usage in
srcwill fail the build.
After each change, run eslint --quiet locally to catch regressions before pushing.
Limitations and Practical Checks
The rule only inspects source files; it cannot detect console calls inserted at runtime via eval or dynamic imports. For those cases, rely on runtime instrumentation or stricter build steps.
To verify that no console calls slip into production, add a quick script in your build pipeline:
grep -R "console." dist/ | wc -l
A non‑zero count indicates a missed lint violation. Combine this with the CI linter to double‑check.
Conclusion
The no-console rule is a lightweight gatekeeper for production quality. By configuring it to allow only necessary methods, using environment overrides, and verifying through CI, teams can keep debugging output out of user traffic while still enjoying console logs during development. Remember to keep your config files isolated and double‑check the environment flags to avoid accidental console leaks.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.