ESLint reports 'Unexpected console statement' in a Node.js project
Unexpected console statement no-consoleShort answer
The message comes from ESLint's no-console rule. The rule page says Node.js projects most likely do not want it. Set 'no-console' to 'off' in your config, keep it on but allow console.warn and console.error with the allow option, or silence a single line with an eslint-disable-next-line comment.
Fix it fast
1Find out which cause you have
Find out which config turns the rule on, so you change the right place.
grep -rn "no-console" eslint.config.js .eslintrc* package.json 2>/dev/null2Match what you see, then jump to its fix
- 1
Every console.log in the Node.js app is flaggedThe rule is on for the whole project, but this is server codeFix - 2
console.log should be flagged, console.warn and console.error should notYou want to keep the rule but still allow warn and errorFix - 3
One intentional console call in an otherwise clean fileOnly one statement needs to be exemptFix
- 1
What does the error look like?
Unexpected console statement no-console
Cause 1 of 3
The rule is on for the whole project, but this is server code
1Check
The ESLint docs say that in Node.js, console is used to output information to the user, and that Node.js developers most likely do not want the rule. Check your config for no-console.
grep -n "no-console" eslint.config.js .eslintrc* package.json 2>/dev/null2Fix
Turn the rule off in eslint.config.js.
// eslint.config.js
import { defineConfig } from "eslint/config";
export default defineConfig([
{
rules: {
"no-console": "off",
},
},
]);3Verify
Lint again; the console warnings are gone.
npx eslint .Confirmed by eslint.org
Cause 2 of 3
You want to keep the rule but still allow warn and error
1Check
List the console methods the code really uses. The no-console rule takes an allow option with the console methods that are permitted.
grep -rn "console\." src/2Fix
Put the allow list in the rule's configuration.
// eslint.config.js
export default [
{
rules: {
"no-console": ["error", { allow: ["warn", "error"] }],
},
},
];3Verify
console.warn and console.error pass; console.log still fails.
npx eslint src/Confirmed by eslint.org
Cause 3 of 3
Only one statement needs to be exempt
1Check
ESLint accepts disable comments for the next line or for the same line.
grep -n "console\." src/index.js2Fix
Add the comment above the statement.
// eslint-disable-next-line no-console
console.error("startup failed");3Verify
Run ESLint on that file.
npx eslint src/index.jsConfirmed by eslint.org
How do I stop it coming back?
- Decide once per project whether console is allowed on the server and write it in the shared config.
- Use a real logger for production output and keep console for scripts and tools.
- Scope exceptions with a files override instead of switching the rule off everywhere.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix