Using Mocha and ES6,

mocha --require react-native-mock/mock.js --compilers js:babel-core/register --recursive test/**/*.js

I got this error:

import createIconSet from './lib/create-icon-set';
SyntaxError: Unexpected token import
    at Object.exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:513:28)
    at Module._extensions..js (module.js:550:10)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/chen/repo/supplychain/beaconRN/node_modules/babel-core/node_modules/babel-register/lib/node.js:166:7)

After some searching I noticed Babel does not handle ES6 code in node_modules/, which uses .js as an extension:

And the code proved the behavior:

function shouldIgnore(filename) {
  if (!ignore && !only) {
    return getRelativePath(filename).split(path.sep).indexOf("node_modules") >= 0;
  } else {
    return util.shouldIgnore(filename, ignore || [], only);

function loader(m, filename) {
  m._compile(compile(filename), filename);

function registerExtension(ext) {
  let old = oldHandlers[ext] || oldHandlers[".js"] || require.extensions[".js"];

  require.extensions[ext] = function (m, filename) {
    if (shouldIgnore(filename)) {
      old(m, filename);
    } else {
      loader(m, filename, old);

Our toolchains is too complicated driven by the eager of Facebook. Just don't make it worse by putting Babel in node_modules.

Write your comment…

Therefore I use ES6 for development and push it on GitHub. And precompile this codebase with babel to ES5 code and publish it on npm – without the ES6 code. Additionally the pkg.main refers to the ES5 entry point :)

Show all replies

Jep of course. If you install a package w/ npm, you wanna start directly and don't wanna debug these packages too :P

Reply to this…

I just see the problem with import/export. Other parts work well for me. However, I think we should use native features only, what do not require transpiler.

However, people want ES6 or even ES7, and I want CoffeeScript and ClojureScript, there are lots of languages out there, and most people just hate ES5.

Reply to this…