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.

kakahan
  • 5
  • 2
  • I’m not sure static constructors exist in JS? https://stackoverflow.com/questions/35156270/static-constructor-in-javascript-es6 – evolutionxbox Dec 19 '21 at 14:08
  • The constructor is a property of the prototype object, you can't use "static" constructor. – Teemu Dec 19 '21 at 14:09
  • 1
    What version of Node? Static blocks are 16+. – Dave Newton Dec 19 '21 at 14:09
  • I am talking about the initialization block https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Class_static_initialization_blocks Edit: node version is 16.7.0 – kakahan Dec 19 '21 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) – evolutionxbox Dec 19 '21 at 14:11
  • Just realized, this is the first time I'm using it on Node. All of the times before I've used them on plain JS on the browser. I dont see how it makes a difference though. I will try to update my node and see if it makes a difference. – kakahan Dec 19 '21 at 14:14
  • It makes a difference because JS changes :) – Dave Newton Dec 19 '21 at 14:22

1 Answers1

0

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

Sandeep Patel
  • 4,815
  • 3
  • 21
  • 37