Node.js stops with 'Invalid shorthand property initializer' on an object literal
SyntaxError: Invalid shorthand property initializerShort 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.
Fix it fast
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.js2Match what you see, then jump to its fix
What does the error look like?
SyntaxError: Invalid shorthand property initializerFirefox 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.js2Fix
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.jsConfirmed by developer.mozilla.org
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.js2Fix
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
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 fixWeb apps
Apollo Server rejects a request as a potential CSRF
HTTP 400 Bad Request from the GraphQL endpoint
Apollo Server's CSRF prevention is on by default.
3 causes, each with a fixWeb apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fix