Skip to content

Using Traceur with Node.js

Domenic Denicola edited this page Jun 22, 2014 · 11 revisions

Using Traceur 0.0.20 or later.

Traceur allows you to hook into Node.js' require function to allow 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 {configFile} from './resources/b'; // use import for ES6 modules
var fs = require('fs');                   // still need to use require for non-ES6 modules

console.log(fs.readFileSync(configFile, 'utf-8'));
// resources/b.js
export var configFile = 'config.json';
// bootstrap.js
var traceur = require('traceur');
traceur.require.makeDefault(function(filename) {
  // don't transpile our dependencies, just our app
  return filename.indexOf('node_modules') === -1;
});
require('./test');
$ node bootstrap.js
BBB

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

Clone this wiki locally