m90
m90

Reputation: 11822

How do I use babel in a node CLI program?

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?

Upvotes: 0

Views: 1435

Answers (1)

loganfsmyth
loganfsmyth

Reputation: 161467

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)/
});

Upvotes: 3

Related Questions