Skip to content

Using Traceur with Node.js

MajorBreakfast edited this page May 1, 2014 · 11 revisions

Using Traceur 0.0.20 or later.

Traceur allows you to hook into Node.js' require function which allows you to require ES6 modules just as if they were Node.js modules.

To do this you can either use traceur.require or you can make traceur.require the default by using traceur.require.makeDefault. makeDefault optionally takes a filter function that takes the path to the file being required as input. If this function returns true Traceur will transform the file. Note: If you call makeDefault multiple times then traceur transpiles the file if at least one of the filter functions returned true.

Below is a more complete example:

// test.js
import {b} from './resources/b';
console.log(b);

// resources/b.js
export var b = 'BBB';

// bootstrap.js
var traceur = require('traceur');
traceur.require.makeDefault(function(filename) {
  // Change this to something more meaningful.
  return filename.endsWith('test.js') || filename.endsWith('b.js');
});
require('./test');
$ node bootstrap.js
BBB

Note: The endsWith method gets injected into String.prototype with require('traceur').

Clone this wiki locally