Skip to content

Wrong error report message for expression {}) #46877

Description

@tuannd89

Version

v19.7.0

Platform

No response

Subsystem

No response

What steps will reproduce the bug?

$ node
> {})
({}))
    ^
Uncaught SyntaxError: Unexpected token ')'

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

> {})
{})
    ^
Uncaught SyntaxError: Unexpected token ')'

What do you see instead?

> {})
({}))
    ^
Uncaught SyntaxError: Unexpected token ')'

Additional information

We got similar result with expression {}]

Activity

  1. added
    replIssues and PRs related to the REPL subsystem.
    on Feb 28, 2023
  2. aduh95 commented on Feb 28, 2023

    @aduh95
    Contributor

    Any input that starts with a { and doesn't end with a ; will be wrapped in parentheses:

    node/lib/repl.js

    Lines 417 to 426 in e4e8421

    // It's confusing for `{ a : 1 }` to be interpreted as a block
    // statement rather than an object literal. So, we first try
    // to wrap it in parentheses, so that it will be interpreted as
    // an expression. Note that if the above condition changes,
    // lib/internal/repl/utils.js needs to be changed to match.
    if (RegExpPrototypeExec(/^\s*{/, code) !== null &&
    RegExpPrototypeExec(/;\s*$/, code) === null) {
    code = `(${StringPrototypeTrim(code)})\n`;
    wrappedCmd = true;
    }

    I think it makes sense for Node.js to show the string it tried to parse rather than the input string – and I think adding those parentheses is useful as well. So if you ask me, I'd say this is not a bug, this is a feature.

  3. cola119 commented on Mar 1, 2023

    @cola119
    Member

    Potentially related issue: #46196

  4. jenthone commented on Mar 1, 2023

    @jenthone
    Contributor

    Try to wrap input in parentheses cause another issue:

    $ node
    > {}),({}
    {} // Expected result is Uncaught SyntaxError: Unexpected token ')'
    
  5. BridgeAR commented on Sep 11, 2025

    @BridgeAR
    Member

    This got fixed by #59607

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    replIssues and PRs related to the REPL subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions