简体   繁体   English

node.js / ES6 /类创建:SyntaxError:意外的保留字

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

I try to create a class on my node.js / express app. 我尝试在我的node.js / express app上创建一个类。

It works in basic js / prototype mode such as : 它适用于基本的js / prototype模式,例如:

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 ? 想要正确启动我的应用程序的想法吗?

You can do this with io.js 你可以用io.js做到这一点

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

Or on node.js with traceur 或者在带有traceur的 node.js上

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 编辑:您可以从此处查看兼容性列表

You need a newer version of nodejs. 您需要更新版本的nodejs。 The class keyword is supported in 4.4.x , but I'm personally seeing it work in v4.2.6. 4.4.x中支持 class关键字,但我个人认为它在v4.2.6中有效。 (Not entirely sure what version of v8 released it, which is what would tell the node version.) (不完全确定哪个版本的v8发布了它,这将告诉节点版本。)

I had this problem. 我有这个问题。

It was caused because I downloaded nodejs' source code than built/ compiled it on my Ubuntu. 这是因为我下载了nodejs的源代码而不是在我的Ubuntu上构建/编译它。 ./configure then make and make install . ./configure然后makemake install

For some reason the ES6 reserved words like class and extends were throwing SyntaxError: Unexpected reserved word, even when using --harmony flag. 出于某种原因,ES6保留的classclassextends都抛出了SyntaxError:意外的保留字,即使使用--harmony flag也是如此。

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 --harmony flag. 现在即使没有--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。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM