Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Babel require hook

Starting a new project and I'm wanting to use ES6/2015 features with Node. Currently trying use the babel-core require hook. This seems simple enough, but my required modules are throwing syntax errors.

App entry point:

require("babel-core/register")({
    ignore: false
});

var app = require('app/app');
...

Required file (app/app.js):

// this SHOULD be compiled by the Babel require hook
app.get('/', (req, res, next) => res.render('page/page', {}));

Syntax Error:

app.get('/', (req, res, next) => res.render('page/page', {}));
                              ^^
SyntaxError: Unexpected token =>

Babel-core: 6.1.21

I am sure I am missing something really simple. Any help would be greatly appreciated. Thanks!

like image 206
chooooons Avatar asked Feb 23 '26 01:02

chooooons


1 Answers

Babel core needs a preset to transform something. If you want to use ES2015, this preset is called babel-preset-es2015.

Add this preset to your project : npm install --save-dev babel-preset-es2015

With babel 6 you also need to create a .babelrc file in your root folder. Add es2015 to use babel-preset-es2015.

{
  "presets": ["es2015"]
}
like image 147
Fabien Sa Avatar answered Feb 25 '26 15:02

Fabien Sa