简体   繁体   中英

Babel 6 - Enable default parameters for functions

I have a piece of code:

'use strict';

class ArticleModel {

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

}

module.exports = ArticleModel

which results in the error Unexpected token = - I don't believe Babel is parsing this. Which babel 6 plugin is needed to parse default parameters in a function?

Edit 1 - this is my .babelrc file

{
  "presets": [
    "es2015",
    "stage-0"
  ]
}

Edit 2 - I am not running babel from the same directory as .babelrc . I'm running babel from inside test/ where the structure looks like this:

/app
/test
/test/runner.js < -- this is what calls babel-core/register
.babelrc

Do I need to explicitly tell babel-core/register where .babelrc is? I assumed it rolled up a directory for it.

Edit 3 - changed babel/register to babel-core/register . Still get the same issue.

npm install babel-preset-es2015 --save-dev

Add the following line to your .babelrc file:

{
  "presets": ["es2015"] 
}

Did you try this?

How are you importing the module into the test? I had a similar problem when my tests started to break after upgrading from Babel 5 to 6. In my case it turned out that the problem was because the import has to referenced the default property in the imported lib.

The initiator of this Babel issue gives a good example: https://github.com/babel/babel/issues/2679

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM