Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

ESLint reports 'Unexpected console statement' in a Node.js project

ErrorUnexpected console statement no-console

Short 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.

  • Applies to ESLint 9, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 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/null
  2. 2Match what you see, then jump to its fix

    1. 1Every console.log in the Node.js app is flaggedThe rule is on for the whole project, but this is server codeFix
    2. 2console.log should be flagged, console.warn and console.error should notYou want to keep the rule but still allow warn and errorFix
    3. 3One intentional console call in an otherwise clean fileOnly one statement needs to be exemptFix

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/null

2Fix

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

Did this fix it?

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

Did this fix it?

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.js

2Fix

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.js

Confirmed by eslint.org

Did this fix it?

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 fix

Back 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 fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.