Chris Abrams Chris Abrams - 4 months ago 13x
Node.js Question

Unexpected token =

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.

Unexpected token =
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


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 || {};