Search code examples
javascriptbabeljseventemitter

Trying to get Facebook Emitter working


I get the following error when trying Facebook emitter from node command line. I tried removing {} around EventEmitter without Bable, then with Bable and the code below.

EventEmitter

Setup, as per: Using Babel. How to use Babel with your tool of choice

npm install fbemitter
npm install babel-register
npm install babel-preset-es2015 --save-dev
echo '{ "presets": ["es2015"] }' > .babelrc

Node error:

$ node emit-node.js
/Users/carlf/Documents/dev/test/emitter/emit-node.js:3
var {EventEmitter} = require('./node_modules/emitter');
    ^
SyntaxError: Unexpected token {

My code:

require("babel-register");

var {EventEmitter} = require('./node_modules/emitter');
var emitter = new EventEmitter();

emitter.addListener('event', function(x, y) { console.log(x, y); });
emitter.emit('event', 5, 10);  // Listener prints "5 10".

FILE: .bablerc

{ "presets": ["es2015"] }

Solution

  • To include it you will need to require it at the top of the entry point to your application.

    If you use babel-register, it won't apply the transforms on the file that calls require("babel-register"); itself. Thus you would need to move that call to a separate file and then call the es2015 code.

    // file1.js
    require("babel-register");
    require('./file2.js');
    
    // file2.js
    // other code
    

    It probably could be more clear - maybe even and example like I posted above. Feel free to send a PR or I'l fix something up later.