2

My current test of Babel is simple.

require("babel-core/register");
import Twitter from "twitter";

I get the error SyntaxError: Unexpected token import, which indicates that I have not gotten Babel to work correctly.

When running node index.js it does not work. It does work when running babel-node index.js

I thought the require hook was supposed to allow ES6 to work at any time. If it only works when using babel-node, can someone explain why? Thank you!

My .babelrc file is

{
  "presets": [
    "es2015"
  ]
}

My dependencies in my package.json are

"dependencies": {
    "babel-cli": "^6.3.17",
    "babel-core": "^6.3.26",
    "babel-preset-es2015": "^6.3.13",
    "babel-register": "^6.3.13",
    "twitter": "^1.2.5"
  }
Connorelsea
  • 2,308
  • 6
  • 26
  • 47
  • Duplicate of this question: http://stackoverflow.com/questions/29207878/requirebabel-register-doesnt-work – arcseldon Dec 27 '15 at 11:05

2 Answers2

3

babel-register will not change the current file, but all subsequent required files. It means you can do something like this:

require('babel-core/register');
require('./app.js');

And use ES6 in app.js and in everything that's required from there.

sdgluck
  • 24,894
  • 8
  • 75
  • 90
Michał
  • 895
  • 6
  • 15
1

For specific documentation on the Babel require hook, please see here

Install:

$ npm install babel-register

Usage:

require("babel-register");

Note that require('babel/register') doesn't transpile the file it is called from. If you want app.js to be included in on-the-fly transpilation, you should execute it with babel-node (Babel's CLI replacement for node).

arcseldon
  • 35,523
  • 17
  • 121
  • 125