How do I use babel in a node CLI program?

情到浓时终转凉″ 提交于 2019-12-04 06:30:53

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/31447120/how-do-i-use-babel-in-a-node-cli-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!