0

I'm writing a small CLI tool in node and would like to use ES6 for that.

index.js looks like:

#!/usr/bin/env node

require('babel/register');
module.exports = require('./app');

I can easily invoke that using

$ node index.js --foo some --bar thing

In my package.json I declare the following:

  "bin": {
    "my-tool": "./index.js"
  }

When installing and executing this it seems that babel is not working as I am getting:

/usr/lib/node_modules/my-tool/app.es6:1
(function (exports, require, module, __filename, __dirname) { import yargs fro
                                                          ^^^^^^
SyntaxError: Unexpected reserved word

What is it that I am missing here?

m90
  • 11,434
  • 13
  • 62
  • 112
  • Not sure why this happens (I can reproduce). As a quick fix, perhaps you can use `#!/usr/bin/env babel-node` instead? – robertklep Jul 16 '15 at 08:54
  • I don't think babel's on-the-fly compilation works with module syntax. – Bergi Jul 16 '15 at 15:25
  • Then I don't understand what it does at all: where is the difference when invoked via `node index.js` (working) and `my-tool` (not working)? – m90 Jul 16 '15 at 15:27

1 Answers1

3

The require hook is meant more for local development than for production use. Ideally you'd precompile before distributing your package. You are running into https://github.com/babel/babel/issues/1889.

You'll need to explicitly tell the register hook not to ignore your application.

require('babel/register')({
  ignore: /node_modules\/(?!my-tool)/
});
loganfsmyth
  • 156,129
  • 30
  • 331
  • 251