导入Meteor中的ES6 npm模块时出现SyntaxError错误
- 我使用
meteor create
了一个新的meteor项目 - 我运行了
npm install -S spacy-nlp
,其中包含一些ES6代码 - 在我的
server/main.js
,我写import spacy from 'spacy-nlp'
- 运行
meteor
,它抱怨Error: The babel-runtime npm package could not be found in your node_modules. Please run the following command to install it: meteor npm install --save babel-runtime
Error: The babel-runtime npm package could not be found in your node_modules. Please run the following command to install it: meteor npm install --save babel-runtime
- 我安装了
babel-runtime
-
然后它抱怨
W20161120-16:40:30.175(8)? (STDERR) /Users/prashanthcr/code/es6-meteor-test/node_modules/spacy-nlp/src/start-io.js:3 W20161120-16:40:30.176(8)? (STDERR) const { spawn } = require('child_process') W20161120-16:40:30.176(8)? (STDERR) ^ W20161120-16:40:30.177(8)? (STDERR) W20161120-16:40:30.178(8)? (STDERR) SyntaxError: Unexpected token {
不知道从哪里去。 为什么我不能导入使用ES6的npm包?
我在全球安装了Node.js 7.1.0,并使用Meteor 1.4.2.3。
meteor不会将node_modules
任何文件编译为ES5。 这意味着代码运行不变。 这就是你在运行时得到错误的原因,而不是在构build过程中。
Meteor v1.4.2.x使用节点v4.6.2(您可以使用meteor node --version
检查节点版本)。
这个版本的节点通常不支持解构赋值( const {foo} = ...
语法)。 这需要使用--harmony_destructuring
标志。 您可以通过运行版本为4.x和6.x的节点shell(REPL)来轻松地进行testing。
在3个terminal会话中input以下内容:
-
$ meteor node
-
$ meteor node --harmony_destructuring
-
$ node
,当节点在v6 +
码:
let bar = () => ({foo: 3}); // 1 let { foo } = bar(); // 2 eval("let { foo } = bar();"); // 3
- 陈述1将运行良好。
- 语句2将不会被节点v4识别(期望更多的用户input),并且将按照节点v6和v4的标志进行预期评估。
- 语句3强制节点将expression式作为一个单元来计算,而不需要用户进一步的input,所以它会引起节点v4抛出你正在看到的错误,并且会被节点v6和v4 w / flag正确评估。
这意味着你正面临的错误是一个节点问题,你需要:
- find一种方法让Meteor用
--harmony_destructuring
标志运行节点。 - 叉包,并改变有问题的expression式。
- 分叉并将构buildconfiguration添加到包(例如,预发布步骤)。
- 等待Meteor开始使用Node v6(应该很快就会发生,因为它现在是稳定的LTS版本)。
这里有一个关于babel-runtime的故事,这对我来说似乎有点困惑,但我认为解决scheme就是这样做
meteor npm install --save babel-runtime
https://forums.meteor.com/t/meteor-1-4-2-1-is-an-important-patch-for-1-4-2-users/31190