babel-node无法在node_modules中要求jsx文件
我知道默认情况下, babel-node
忽略了node_modules
,所以我运行了三种不同的方式来覆盖它,都失败了:
-
用
.babelrc
运行babel-node app.js
:{ "presets": ["es2015", "react"], "only": [ "app", "node_modules/react-components" ] }
结果:
SyntaxError: Unexpected token <
用于所需的jsx节点模块 -
用
.babelrc
运行babel-node app.js
:{ "presets": ["es2015", "react"], "ignore": "node_modules\/(?!react-components)" }
result:
SyntaxError: Unexpected token <
用于require jsx节点模块 -
使用
babel-node ./bin/www --ignore '/node_modules/(?!react-components)
运行babel-node ./bin/www --ignore '/node_modules/(?!react-components)
:{ "presets": ["es2015", "react"] }
结果:
[project]/node_modules/babel-preset-react/node_modules/babel-plugin-transform-react-jsx/lib/index.js:12 var visitor = require("babel-helper-builder-react-jsx")({ ^ TypeError: object is not a function
与忽略选项使用注册挂钩工作正常。
在node app.js
的开头使用这段代码运行node app.js
app.js
require('babel-core/register')({ ignore: /node_modules\/(?!react-components)/ });
即使这个工程,我仍然想知道为什么我的实施与babel-node
不起作用。 谢谢。
引用:
- 如何在节点CLI程序中使用babel?
- 使用babel从node_modules导入模块,但失败
- http://babeljs.io/docs/usage/cli/
- http://babeljs.io/docs/usage/options/
这是巴贝尔的一个已知的错误,它只忽略和ignore
。
相关的bug是T6726 ,已经在babel 6.14.0中修复了 。