最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Babel 6 - Enable default parameters for functions - Stack Overflow

matteradmin6PV0评论

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.

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.

Share Improve this question edited Nov 10, 2015 at 4:04 Guy 67.5k101 gold badges265 silver badges332 bronze badges asked Nov 9, 2015 at 13:55 Chris AbramsChris Abrams 42.6k20 gold badges54 silver badges57 bronze badges 2
  • 'babel/register' doesn't exist anymore? should be 'babel-core/register' – Seneca Commented Nov 9, 2015 at 14:17
  • Tks @Seneca but I still get the same error even when changing it to babel-core/register. – Chris Abrams Commented Nov 9, 2015 at 14:24
Add a ment  | 

2 Answers 2

Reset to default 5
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./babel/babel/issues/2679

Post a comment

comment list (0)

  1. No comments so far