unexpected token = for class properties in node 8.4

running the following code in node (v8.4)

class TodoStore {
    todos = [];

    get completedTodosCount() {
        return this.todos.filter(
            todo => todo.completed === true

    report() {
        if (this.todos.length === 0)
            return "<none>";
        return `Next todo: "${this.todos[0].task}". ` +
            `Progress: ${this.completedTodosCount}/${this.todos.length}`;

    addTodo(task) {
            task: task,
            completed: false,
            assignee: null

const todoStore = new TodoStore();

todoStore.addTodo("read MobX tutorial");

todoStore.addTodo("try MobX");

todoStore.todos[0].completed = true;

todoStore.todos[1].task = "try MobX in own project";

todoStore.todos[0].task = "grok MobX tutorial";

gives me the following error:

        todos = [];

SyntaxError: Unexpected token =
    at createScript (vm.js:74:10)
    at Object.runInThisContext (vm.js:116:10)
    at Module._compile (module.js:537:28)
    at Object.Module._extensions..js (module.js:584:10)
    at Module.load (module.js:507:32)
    at tryModuleLoad (module.js:470:12)
    at Function.Module._load (module.js:462:3)
    at Function.Module.runMain (module.js:609:10)
    at startup (bootstrap_node.js:158:16)
    at bootstrap_node.js:598:3

Support for instance class fields starts with node >= 12.

Literal class properties are not supported by any version of node, according to this table. You’ll still have to set any instance properties inside your class constructor:

class TodoStore {

    constructor() {
        this.todos = [];
    // ...

If you wish to define a static property, you’d assign that directly to the TodoStore reference, after the class has been declared:

TodoStore.todos = [];

Instance class fields will be supported as of Node v12, so one solution is to use a version >= 12 once it is released.


For now if you’re interested the nightly builds are at: https://nodejs.org/download/nightly/

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .
Read More:   Detect when window vertical scrollbar appears

Similar Posts