我知道babel-node默认忽略node_modules,所以我用三种不同的方式来覆盖它,都失败了:
>用.babelrc运行babel-node app.js:
{ "presets": ["es2015","react"],"only": [ "app","node_modules/react-components" ] }
结果:SyntaxError:意外的令牌<对于所需的jsx节点模块
>用.babelrc运行babel-node app.js:
{ "presets": ["es2015","ignore": "node_modules\/(?!react-components)" }
结果:SyntaxError:意外的令牌<对于require jsx节点模块
>使用.babelrc运行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
使用带有ignore选项的register hook正常工作.
在app.js的开头使用此代码运行节点app.js.
require('babel-core/register')({ ignore: /node_modules\/(?!react-components)/ });
即使这有效,我仍然想知道为什么我的babel-node实现不起作用.谢谢.
引用:
> How do I use babel in a node CLI program?
> import a module from node_modules with babel but failed
> http://babeljs.io/docs/usage/cli/
> http://babeljs.io/docs/usage/options/