如何在节点CLI程序中使用babel?

时间:2022-02-09 18:52:09

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

我正在节点中编写一个小型CLI工具,并希望使用ES6。

index.js looks like:

index.js看起来像:

#!/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:

在我的package.json中,我声明了以下内容:

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

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

当安装和执行它时,似乎babel不能正常工作:

/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 个解决方案

#1


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.

require钩子对于本地开发而言比对生产使用更多。理想情况下,在分发包之前,您需要进行预编译。您正在遇到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)/
});

#1


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.

require钩子对于本地开发而言比对生产使用更多。理想情况下,在分发包之前,您需要进行预编译。您正在遇到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)/
});