0

I've been looking at this error for a long time and I just cannot wrap my head around it.

Here's the code:

class Instance {

    static {

    }

}

module.exports = Instance;

That is all there is. The class was full of stuff but I deleted them all because they are irrevelant. It's giving the same error with or without them.

The error:

    static {
           ^

SyntaxError: Unexpected token '{'
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1031:15)
    at Module._compile (node:internal/modules/cjs/loader:1065:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:94:18)
    at Object.<anonymous> (path\to\project\index.js:1:18)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)

I've used static constructors in the past and testing the same code somewhere else seems to work so I dont understand what the issue is here. Please help me.

SOLUTION: Updated Node to 16.13.1 and it now works. 16.7.0 wasnt enough.

7
  • I’m not sure static constructors exist in JS? stackoverflow.com/questions/35156270/… Commented Dec 19, 2021 at 14:08
  • The constructor is a property of the prototype object, you can't use "static" constructor. Commented Dec 19, 2021 at 14:09
  • 1
    What version of Node? Static blocks are 16+. Commented Dec 19, 2021 at 14:09
  • I am talking about the initialization block developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Edit: node version is 16.7.0 Commented Dec 19, 2021 at 14:10
  • It seems to be a ES2022 feature. I can’t find evidence that it’s been implemented in node yet (although I’m likely wrong) Commented Dec 19, 2021 at 14:11

2 Answers 2

1

It is supported by Node 17.3.0. It is an ES2022 feature. This https://node.green/#ES2022 is a nice link to find ES feature compatibility in Nodejs. You will have to download the current version of Nodejs which is 17.3.0. enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

This is the answer. However just updating to 16.13.1 fixed the issue for me.
0

This happened on my workspace too, but it was really a case of shooting myself in the foot with the workaround for another issue :)

To recover, I just ran echo PATH=${PATH} > ~/.huskyrc again. And yeah, I'm looking forward to not requiring workarounds.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.