Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexpected token =

Tags:

node.js

I'm unsure as to what could cause this error in Node.js, as I've never seen it before and cannot find another issue online.

Message:
    Unexpected token =
Stack:
SyntaxError: Unexpected token =
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:404:25)
    at Object.Module._extensions..js (module.js:432:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:311:12)
    at Module.require (module.js:366:17)
    at require (module.js:385:17)
    at Object.<anonymous>     (/Projects/api/test/integration/models/article.js:3:15)

The file that is causing the error has the following contents:

'use strict';

var Article = require('../../../models/article')

Why in the world would = cause an error?

Edit 1 - adding the article.js that is being required:

'use strict';

class ArticleModel {

  constructor(options = {}) {
    this.options = options
  }

}

module.exports = ArticleModel
like image 606
Chris Abrams Avatar asked Nov 09 '15 04:11

Chris Abrams


1 Answers

node.js 5.0 does not support all ES6 features yet. In particular, it does not yet support default parameters.

So this line:

constructor(options = {}) {

is what is causing the error with the = assignment.

See this table for which features are supported in node.js 5.0.


You can replace the default parameter assignment with the old fashioned method:

constructor(options) {
    this.options = options || {};
}
like image 69
jfriend00 Avatar answered Oct 05 '22 03:10

jfriend00