-
Notifications
You must be signed in to change notification settings - Fork 583
Using Traceur with Node.js
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. (Note that the reverse process of using existing nodejs modules in ES6 is not yet supported in Traceur, see systemjs instead).
To do this you can either use traceur.require
or you can make traceur.require
the default by using traceur.require.makeDefault([filter, options])
. Options are enumerated in Options.js. 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 {readFileSync} = require('fs'); // use require for non-ES6 Node modules
console.log(readFileSync(configFile, 'utf-8'));
// resources/b.js
export var configFile = 'package.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
{"contentsOf":"configFile"}
Note: The endsWith
method gets injected into String.prototype
with require('traceur')
.