javascript node.js / ES6 / 类创建:语法错误:意外的保留字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27177485/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:05:02  来源:igfitidea点击:

node.js / ES6 / class creation : SyntaxError: Unexpected reserved word

javascriptnode.jsexpressecmascript-6ecmascript-harmony

提问by ceadreak

I try to create a class on my node.js / express app.

我尝试在我的 node.js/express 应用程序上创建一个类。

It works in basic js / prototype mode such as :

它在基本的 js / 原型模式下工作,例如:

function MyClass() { 
    /* constructor code */
};

MyClass.prototype.myMethod = function() {
    /* method code */
};

module.exports = MyClass;

But I want to do use the class, constructor, extends, ... keywords.

但我想使用 class、constructor、extends、... 关键字。

I've try that :

我试过了:

class MyClass {
    constructor() {
        /* constructor code */
    }

    myMethod() {
        /* method code */
    }

}

But it doesn't work, the error is :

但它不起作用,错误是:

class MyClass {
^^^^^
SyntaxError: Unexpected reserved word

My command line to launch the app with all harmony options :

我使用所有和声选项启动应用程序的命令行:

node `node --v8-options | grep harmony | cut -d ' ' -f | xargs` my-app.js 

An idea to launch my app correctly please ?

请正确启动我的应用程序的想法?

回答by SerkanSerttop

You can do this with io.js

你可以用io.js做到这一点

iojs --use_strict --harmony_classes my-app.js

Or on node.js with traceur

或者在 node.js 上使用traceur

var traceur = require('traceur');
traceur.require.makeDefault(function(file) {
  return file.indexOf('node_modules') == -1;
});

require('./my-app').run();

Make sure to test the new features before using them, some are not supported. Edit: You can check the compatibility list from here

确保在使用新功能之前对其进行测试,有些功能不受支持。编辑:您可以从此处查看兼容性列表

回答by ThatBlairGuy

You need a newer version of nodejs. The classkeyword is supported in 4.4.x, but I'm personally seeing it work in v4.2.6. (Not entirely sure what version of v8 released it, which is what would tell the node version.)

您需要更新版本的 nodejs。该class关键字在 4.4.x 中受支持,但我个人认为它在 v4.2.6 中有效。(不完全确定哪个版本的 v8 发布了它,这会告诉节点版本。)

回答by l__flex__l

I had this problem.

我有这个问题。

It was caused because I downloaded nodejs' source code than built/ compiled it on my Ubuntu. ./configurethen makeand make install.

这是因为我下载了 nodejs 的源代码而不是在我的 Ubuntu 上构建/编译它。./configure然后makemake install

For some reason the ES6 reserved words like classand extendswere throwing SyntaxError: Unexpected reserved word, even when using --harmonyflag.

出于某种原因,ES6 保留字像classextends抛出 SyntaxError: Unexpected reserved word,即使在使用--harmony标志时也是如此。

It was solved by me downloading the nodejs binaries for linux (https://nodejs.org/download/).

我下载了用于 linux 的 nodejs 二进制文件 ( https://nodejs.org/download/)解决了这个问题。

Now class and extends work even without --harmonyflag.

现在即使没有--harmony标志也可以分类和扩展工作。

I believe the issue came about from my building/ compiling process. For some reason the ES6 additions were not built or configured properly.

我相信这个问题来自我的构建/编译过程。出于某种原因,ES6 添加的内容没有正确构建或配置。

The binaries, to my understanding, are built completely and correctly for linux already and therefore the ES6 is added and configured correctly.

据我了解,二进制文件已经完全正确地为 linux 构建,因此 ES6 已正确添加和配置。