15

我正在使用带有标志 child_process 的 ES6 测试节点,--harmony但在我导入时它在第一步失败。有任何想法吗?

import {'spawn'} from child_process;
console.log(spawn);

我跑:

node --harmony test.js

我得到:

:1
(function (exports, require, module, __filename, __dirname) { import {spawn} f
                                                              ^^^^^^
SyntaxError: Unexpected token import
4

2 回答 2

6

Not all features of ES6 are supported in v8 and thus in Node. Modules I believe are one of those things.

You might find this compatibility matrix handy: http://kangax.github.io/es5-compat-table/es6/

You could use the Traceur compiler to do this, although not everything is implemented with modules yet it appears

Edit in Dec 2015 - as some comments to this answer suggest, at this point, Babel has become the preferred ES6 transpiler. At the time I wrote my original answer, Babel was still known as 6to5 and was not as popular as Traceur. Today, Babel is the go-to choice.

于 2014-04-14T21:47:07.940 回答
0

试试--experimental-modules国旗

早在 2017 年,Node.js 8.9.0 就提供了对 ECMAScript 模块的实验性支持,这些模块以其导入和导出语句而闻名。这种支持是标志 --experimental-modules 的背后。

https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff

于 2020-01-16T17:04:12.180 回答