javascript - 在 Node 4.x 中导出 ES6 类意外的保留字

标签 javascript node.js ecmascript-6

我在 Node 脚本中有以下内容:

"use strict";

class Whatever {
    constructor() {
        console.log("I'm in the constructor!");
    }
}

export default Whatever;

我得到关于 exportUnexpected reserved word

我在这里缺少什么?如何在外部文件中指定类定义并包含/需要它?

最佳答案

Node.js 默认不支持 ES6 模块。您需要使用 --harmony--harmony_modules 标志来激活它们。默认是 CommonJS 声明 (require/module.exports) .

修改您的代码以支持 CommonJS 语法:

"use strict";

class Whatever {
    constructor() {
        console.log("I'm in the constructor!");
    }
}

module.exports = Whatever;

关于javascript - 在 Node 4.x 中导出 ES6 类意外的保留字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33155917/

相关文章:

javascript - Node 中的超测自定义 express 服务器

javascript - 在 ES6 中过滤或映射节点列表

javascript - 当我将其上方的div更改为固定时,如何阻止固有定位的元素跳转页面?

javascript - 我在哪里可以下载 eclipse 的基本服务器配置?

javascript - VSCode 中的流类型检查性能

javascript - 用 ID_x 匹配 ID_x 或用 +i 循环遍历 id

node.js - Mongoose 在嵌套对象上找到一个

node.js - 如何在 `npm outdated` 中包含 Beta 版本?

javascript - ES6 相当于这个 Angular 2 typescript

javascript - 导出与定义内联或在文件末尾的 es6 默认类?