编写 ES6 模块并转换为 CommonJS 时,实际上没有导出指定的导出

When writing an ES6 module and converting to CommonJS, no specified exports are actually being exported

我有一个 ES6 模块,我试图从中导出几个函数。最终目标是导入到纯脚本中,但现在我应该导出的函数甚至没有出现在 Node 中,我不明白为什么。

这是模块:

"use strict";

const point = (x, y) => new Point(x, y)
const pointToString = (point) => point.toString()

class Point {
    constructor(x, y) {
        this.x = x;
        this.y = y;
    }
    toString () {
        return `(${this.x}, ${this.y})`;
    }
}

export { point, pointToString }

我是这样转译的:

browserify src/page.js -t [ babelify --presets [es2015] ] > src/Page.js

然后我尝试将它加载到 Purescript 和 Node.js 中。 Purescript 报告未定义 pointpointToString。节点会话如下所示:

> require("./src/Page.js")
{}
> require("./src/Page.js").point
undefined
> require("./src/Page.js").pointToString
undefined
> 

我完全不知所措。我应该怎么做才能导出这两个函数?

使用babel-cli创建适合Node的CommonJS格式的模块:

babel --presets=es2015 src/page.js > lib/Page.js

将编译后的文件放在单独的目录中通常是个好主意。