简体   繁体   English

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

[英]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. 我正在node中编写一个小型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? 我在这里想念什么?

The require hook is meant more for local development than for production use. require钩不仅用于生产,还用于本地开发。 Ideally you'd precompile before distributing your package. 理想情况下,您应该在分发软件包之前进行预编译。 You are running into https://github.com/babel/babel/issues/1889 . 您正在遇到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)/
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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