繁体   English   中英

编写ES6模块并将其转换为CommonJS时,实际上未导出任何指定的导出

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

我有一个ES6模块,试图从中导出几个功能。 最终目标是导入到purescript中,但是现在我所谓的导出函数甚至都没有出现在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中。 Purescript报告未定义pointpointToString Node会话如下所示:

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

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

使用babel-cli以CommonJS格式创建一个适用于Node的模块:

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

通常将编译文件放入一个单独的目录是一个好主意。

暂无
暂无

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

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