Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

Node.js stops with 'Invalid shorthand property initializer' on an object literal

ErrorSyntaxError: Invalid shorthand property initializer

Short answer

Node.js (V8) throws this SyntaxError when an object literal contains key = value instead of key: value, or a computed key without square brackets. Inside { } use a colon between key and value, wrap expressions in [ ], or build the object first and assign properties afterwards.

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

Fix it fast

  1. 1Find out which cause you have

    Start the app and read the file and line in the error output. Open that line and look inside the nearest { } for an equals sign or an expression used as a key.

    node server.js
  2. 2Match what you see, then jump to its fix

    1. 1The caret points at = inside curly braces, often in an options object or a copied JSON-like blockAn equals sign is used where a colon belongsFix
    2. 2The caret points near a + or another operator inside the bracesA key is built from an expression without square bracketsFix

What does the error look like?

  • SyntaxError: Invalid shorthand property initializer
  • Firefox wording: SyntaxError: missing : after property id

Cause 1 of 2

An equals sign is used where a colon belongs

1Check

Find the object literal on the reported line. Inside { }, every property must be written key: value.

grep -n "{[^}]*[A-Za-z_]\+ *= *[^=]" server.js

2Fix

Replace = with a colon, or create the object first and assign the property with a dot.

// wrong
const user = { name = "Ada" };
// right
const user = { name: "Ada" };
// or
const user2 = {};
user2.name = "Ada";

3Verify

Check the file again, then run it.

node server.js

Confirmed by developer.mozilla.org

Did this fix it?

Cause 2 of 2

A key is built from an expression without square brackets

1Check

Look for a key that is not a plain name or a quoted string, such as "b" + "ar" or a function call.

sed -n '1,40p' server.js

2Fix

Wrap the key expression in square brackets so it is a computed property name.

// wrong
const obj = { "b" + "ar": "foo" };
// right
const obj = { ["b" + "ar"]: "foo" };

3Verify

The object now holds the computed key.

node -e "console.log({ ['b' + 'ar']: 'foo' })"

Confirmed by developer.mozilla.org

Did this fix it?

How do I stop it coming back?

  • Run ESLint or your editor's syntax check so the error shows while you type, not at start-up.
  • Remember the two jobs of the equals sign: it assigns to variables and properties, never inside { } of a literal.
  • Start the app once in CI before deploying, so a syntax error fails the build and not production.

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

Web apps

tsconfig rejects allowImportingTsExtensions

An error that allowImportingTsExtensions needs noEmit or emitDeclarationOnly

allowImportingTsExtensions lets files import each other with .ts or .tsx extensions, and TypeScript only allows it together with noEmit or emitDeclarationOnly.

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