如何编译导入的ES6节点模块?

我有一个用ES6编写的应用程序,我使用babel来转换该应用程序中的所有文件。 这个应用程序还包括一个带有React组件的node_module(也是用ES6编写的)。

我正在导入这样的组件:

import { Box } from 'components/Box' 

当我跑步

 babel-node --presets es2015,stage-0,react 

在导入Box组件的文件上,第1行的组件/ Box文件中出现错误:

 (function (exports, require, module, __filename, __dirname) { import React, { Component } from 'react' ^^^^^^ SyntaxError: Unexpected token import 

我认为这是为什么发生的原因是因为babel-node不是在传输node_modules。 这是有道理的,因为大多数节点模块是以commonjs格式编写的。 有没有办法将这个特定的节点模块列入白名单,以便它可以被传输?

你能提供更多信息吗? 也许你指的其他文件? 你想达到什么目的?

我试着运行下面的package.json ,它工作得很好。

 { "name": "babel-node-test", "version": "1.0.0", "description": "", "main": "something.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "devDependencies": { "babel-preset-es2015": "^6.5.0", "babel-preset-react": "^6.5.0", "react": "^0.14.7" } } 

用这些文件作为testing:

something.js

 import { Box } from './components/Box'; 

components/box.js

 import React, { Component } from 'react'; class Box extends Component{ } export default Box; 

另外,在附注中引用文件:

不意味着生产使用你不应该在生产中使用babel节点。 由于caching被存储在内存中,所以内存使用率很高,这是不必要的。 整个应用程序需要进行即时编译,您也会经常遇到启动性能问题。

为什么不使用这里提到的任何构build系统https://babeljs.io/docs/setup/