如何在节点CLI程序中使用babel? [英] How do I use babel in a node CLI program?
问题描述
我正在节点中编写一个小型CLI工具,并希望使用ES6。
I'm writing a small CLI tool in node and would like to use ES6 for that.
index.js看起来像:
index.js looks like:
#!/usr/bin/env node
require('babel/register');
module.exports = require('./app');
我可以轻松地使用
$ node index.js --foo some --bar thing
在我的 package.json
中我声明如下:
"bin": {
"my-tool": "./index.js"
}
当安装并执行这个时候,似乎babel没有工作,因为我得到:
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
我在这里缺少什么?
推荐答案
需要钩子更适用于本地开发而不是生产用途,理想情况下,您可以在发布包之前进行预编译,您正在进行 https://github.com/babel/babel/issues/1889 。
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)/
});
这篇关于如何在节点CLI程序中使用babel?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!