Home > Net >  Node JS not compiling my private instance variable
Node JS not compiling my private instance variable

Time:11-25

Right now I have a class that is similar to below

class Rectangle {
    #height = 0;
    #width;
    constructor(height, width) {
      this.#height = height;
      this.#width = width;
    }
}

If i tried to compile this with node.js, it gives me the following error.

    #height = 0;
    ^

SyntaxError: Invalid or unexpected token
    at Module._compile (internal/modules/cjs/loader.js:723:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

Does anyone know how to resolve this?

I tried making the variable public and also static, but nothing prevents it from throwing the error above.

CodePudding user response:

Your issue derives most likely from an outdated version of node.js, ensure you have the latest version downloaded.

https://nodejs.org/en/download/

  • Related