简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM